arrays - MongoDB aggregate result with two different keys -
i have put question helped me obtain array obtaining result in aggregation mongodb query ( mongodb aggregate array 2 fields )
my problem right obtain combine 2 different keys.
i have products collection general products on it:
{ "_id" : objectid("554b9f223d77c810e8915539"), "brand" : "airtex", "product" : "e7113m", "type" : "fuel pump", "warehouse_sku" : [ "1_5551536f3d77c870fc388a04", "2_55515e163d77c870fc38b00a" ] } and have 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 want general products _id , inside child products matches specific warehouse_sku pattern. desired result 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 _id general product 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 _id general , child products [don't want it] not _id's general products inside child product:
{ "_id" : objectid("55524d0c3d77c8ba9cb2d9fd") } { "_id" : objectid("554b9f223d77c810e8915539") }
use following aggregation pipeline desired list of objectids. uses $ifnull aggregation operator add _id field if array general_products field 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 give document array list_products objectids:
/* 1 */ { "result" : [ { "_id" : null, "list_products" : [ objectid("554b9f223d77c810e8915539"), objectid("554b9f2e3d77c810e8915549"), objectid("554b99b83d77c810e8915436"), objectid("554b9f173d77c810e8915533"), objectid("554b9f143d77c810e8915530"), objectid("554b9f123d77c810e891552f") ] } ], "ok" : 1 }
Comments
Post a Comment