[英]mongoDB aggregate $lookup and push some info from existing array
I have many collections in my MongoDB database.我的 MongoDB 数据库中有许多 collections。 Aggregation work's good but I can't push some fields to the output I needed,
聚合工作很好,但我无法将某些字段推送到我需要的 output,
collection A is:集合 A 是:
{
_id: some mongodbID
//...fields
items: [
{
_id: someId,
color: someId <---- Im aggregate this with lookup
neededFieldToPush: 123
},
{
_id: someId,
color: someId <---- Im aggregate this with lookup
neededFieldToPush: 566
}
]
}
my query is:我的查询是:
await Invoice.aggregate([
{ $match: query },
{ $unwind: "$items" },
//colors
{
$lookup: {
from: "colors",
localField: "items.itemColor",
foreignField: "_id",
as: "itemColor"
}
},
{
$addFields: {
"prMove.itemColor": { $arrayElemAt: ["$itemColor.colorName", 0] },
}
},
{
$group: {
_id: "$_id",
items: { $push: "$items" }, <-- original items
prMove: { $push: "$prMove" },
}
},
])
.sort({date: -1})
.skip(+req.query.offset)
.limit(+req.query.limit)
I need to have output like this:我需要像这样的 output :
_id: someId,
items: [//original items],
prMove: [
{
itemColor: some color name, <--- it's works fine
neededFieldToPush: 123
},
{
itemColor: some color name, <--- it's works fine
neededFieldToPush: 566
},
]
so, how I can push neededFieldToPush
field into prMove
object?那么,如何将
neededFieldToPush
字段推送到prMove
object 中?
thank you谢谢你
The implementation of your query is expensive because you have used $unwind
stage and $group
stage, it will impact performance,查询的实现很昂贵,因为您使用
$unwind
阶段和$group
阶段,它会影响性能,
The second thing is the $sort
, $skip
and $limit
functions will not work on the aggregate function of mongoose, you have use stages for that,第二件事是
$sort
, $skip
和$limit
功能不适用于 mongoose 的聚合 function,您有使用阶段,
The third thing is to use sort, $skip
and $limit
stages immediately after $match
stage, so it will improve performance while you create an index on required fields.第三件事是在
$match
阶段之后立即使用 sort、 $skip
和$limit
阶段,这样可以在您为必填字段创建索引时提高性能。
Improved query,改进的查询,
$match
your query $match
你的查询$sort
sort stage $sort
排序阶段$skip
stage to pass offset $skip
阶段以通过偏移量$limit
stage to limit documents $limit
阶段限制文件$lookup
with colors collection and pass items.color
as localField
$lookup
使用 colors 集合并将items.color
作为localField
传递$addFields
to edit items
array with color name in items
$addFields
以在items
中使用颜色名称编辑items
数组$map
to iterate loop of items
array $map
迭代items
数组的循环$reduce
to iterate loop of itemColor
array result from lookup, check condition if _id
match then get color name and set value in color
field $reduce
从查找中迭代itemColor
数组结果的循环,检查条件是否_id
匹配,然后获取颜色名称并在color
字段中设置值$mergeObjects
to merge new color
field and current fields of the item object $mergeObjects
合并项目 object 的新color
字段和当前字段$$REMOVE
to remove itemColor
array because it is not needed now $$REMOVE
删除itemColor
数组,因为现在不需要它let offset = req.query.offset;
let limit = req.query.limit;
await Invoice.aggregate([
{ $match: query },
{ $sort: { date: -1 } },
{ $skip: offset },
{ $limit: limit },
{
$lookup: {
from: "colors",
localField: "items.color",
foreignField: "_id",
as: "itemColor"
}
},
{
$addFields: {
items: {
$map: {
input: "$items",
as: "i",
in: {
$mergeObjects: [
"$$i",
{
color: {
$reduce: {
input: "$itemColor",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$i._id"] },
"$$this.colorName",
"$$value"
]
}
}
}
}
]
}
}
},
itemColor: "$$REMOVE"
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.