MongoDb Post formatting of $lookup aggregation with limited data -
i have 2 collection coll1 , coll2. want apply $lookup on fields "_id" , "comm_field" used query:
db.coll1.aggregate([ { $lookup: { from: "coll2", localfield: "_id", foreignfield: "comm_field", as: "inventory_docs" } }, { $project:{"_id" : 0, "inventory_docs" : 1} }, { $unwind:"$inventory_docs"} ])
and output as:
/* 1 */ { "inventory_docs" : { "_id" : objectid("ssdfsfsdfsdfsfsdfsdfsdfsfsdf"), "comm_field" : numberlong(1111), "status" : "active" } } /* 2 */ { "inventory_docs" : { "_id" : objectid("erteterterterterterterterter"), "comm_field" : numberlong(1111), "status" : "active" } } /* 3 */ { "inventory_docs" : { "_id" : objectid("vbvbfvbdbbcvbvcbcdrgvbcbcbcv"), "comm_field" : numberlong(2222), "status" : "active" } }
is there way can see output like:
{ "_id" : objectid("ssdfsfsdfsdfsfsdfsdfsdfsfsdf"), "comm_field" : numberlong(1111), "status" : "active" } { "_id" : objectid("erteterterterterterterterter"), "comm_field" : numberlong(1111), "status" : "active" } { "_id" : objectid("vbvbfvbdbbcvbvcbcdrgvbcbcbcv"), "comm_field" : numberlong(2222), "status" : "active" }
so want output in format {---}, not in {"inventory_docs":{---}}
neil's answer worked. addition of { $replaceroot: { newroot: "$inventory_docs" } }
did job. neil.
db.coll1.aggregate([ { $lookup: { from: "coll2", localfield: "_id", foreignfield: "comm_field", as: "inventory_docs" } }, { $project:{"_id" : 0, "inventory_docs" : 1} }, { $unwind:"$inventory_docs"}, { $replaceroot: { newroot: "$inventory_docs" } } ])
wiki
Comments
Post a Comment