问题
I have recently put this question that helped me to obtain an array for obtaining a result in an aggregation mongoDB query ( MongoDB Aggregate Array with Two Fields )
My problem right now is to obtain combine two different keys.
I have a products collection with general products on it:
{
"_id" : ObjectId("554b9f223d77c810e8915539"),
"brand" : "Airtex",
"product" : "E7113M",
"type" : "Fuel Pump",
"warehouse_sku" : [
"1_5551536f3d77c870fc388a04",
"2_55515e163d77c870fc38b00a"
]
}
And I have the following child product (it has multiple general products on it)
{
"_id" : ObjectId("55524d0c3d77c8ba9cb2d9fd"),
"brand" : "Performance",
"product" : "P41K",
"type" : "Fuel Pump Component",
"general_products" : [
ObjectId("554b9f123d77c810e891552f"),
ObjectId("554b9f143d77c810e8915530"),
ObjectId("554b9f173d77c810e8915533"),
ObjectId("554b99b83d77c810e8915436"),
ObjectId("554b9f2e3d77c810e8915549")
],
"warehouse_sku" : [
"1_555411043d77c8066b3b6720",
"1_555411073d77c8066b3b6728"
]
}
My problem here is I want the general products with _id and inside child products that matches a specific warehouse_sku pattern. The desired result is like this:
{ "_id" : ObjectId("554b9f2e3d77c810e8915549") }
{ "_id" : ObjectId("554b99b83d77c810e8915436") }
{ "_id" : ObjectId("554b9f173d77c810e8915533") }
{ "_id" : ObjectId("554b9f143d77c810e8915530") }
{ "_id" : ObjectId("554b9f123d77c810e891552f") }
{ "_id" : ObjectId("554b9f223d77c810e8915539") }
I have made multiple queries like:
1)
db.products.aggregate([
... {$match:{warehouse_sku: /^1/ }},
... {$unwind:"$general_products"},
... {$group:{_id: "$general_products"}}
... ])
but the _id from general product is not on it:
{ "_id" : ObjectId("554b9f2e3d77c810e8915549") }
{ "_id" : ObjectId("554b99b83d77c810e8915436") }
{ "_id" : ObjectId("554b9f173d77c810e8915533") }
{ "_id" : ObjectId("554b9f143d77c810e8915530") }
{ "_id" : ObjectId("554b9f123d77c810e891552f") }
2)
db.products.aggregate([
... {$match:{warehouse_sku: /^1/ }},
... {$group:{_id: "$_id"}}
... ])
but give me the _id from the general and child products [don't want it] but not the _id's from general products inside the child product:
{ "_id" : ObjectId("55524d0c3d77c8ba9cb2d9fd") }
{ "_id" : ObjectId("554b9f223d77c810e8915539") }
回答1:
Use the following aggregation pipeline to get the desired list of ObjectIds. This uses the $ifNull aggregation operator to add the _id
field if the array general_products
field does not exist:
db.products.aggregate([
{
"$match": {"warehouse_sku": /^1/ }
},
{
"$group": {
"_id": {
"_id": "$_id",
"general_products": "$general_products"
},
"data": {
"$addToSet": "$_id"
}
}
},
{
"$project": {
"_id": 0,
"general_products": {
"$ifNull": ["$_id.general_products", "$data"]
}
}
},
{
"$unwind": "$general_products"
},
{
"$group": {
"_id": null,
"list_products": {
"$addToSet": "$general_products"
}
}
}
]);
This will give you a document with an array list_products
with ObjectIds:
/* 1 */
{
"result" : [
{
"_id" : null,
"list_products" : [
ObjectId("554b9f223d77c810e8915539"),
ObjectId("554b9f2e3d77c810e8915549"),
ObjectId("554b99b83d77c810e8915436"),
ObjectId("554b9f173d77c810e8915533"),
ObjectId("554b9f143d77c810e8915530"),
ObjectId("554b9f123d77c810e891552f")
]
}
],
"ok" : 1
}
来源:https://stackoverflow.com/questions/30241736/mongodb-aggregate-result-with-two-different-keys