Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB aggregation with $lookup only include (or project) some fields to return from query

In mongo, after doing an aggregation with $lookup, I would like the request to return only some fields and not the whole document.

I have the following query :

db.somecollection.aggregate([{     $lookup: {         from: "campaigns",         localField: "campId",         foreignField: "_id",         as: "campaign"     } }, {     $unwind: "$campaign" }, {     $lookup: {         from: "entities",         localField: "campaign.clientid",         foreignField: "_id",         as: "campaign.client"     } }]); 

This request will return me this :

{ "_id" : ObjectId("56cc7cd1cc2cf62803ebfdc7"), "campId" : ObjectId("56c740e4479f46e402efda84"), "articleId" : ObjectId("56c742c06094640103ba3843"), "campaign" : {     "_id" : ObjectId("56c740e4479f46e402efda84"),     "clientid" : ObjectId("56c740b8479f46e402efda83"),     "client" : [         {             "_id" : ObjectId("56c740b8479f46e402efda83"),             "username" : "someusername",             "shhh" : "somehashedpassword",             "email" : "[email protected]",         }     ] } 

The request works well, but I would like to filter the fields in campaign.client to only get for example _id and username. Is there a way to do this in a MongoDB aggregate request?

like image 575
Samuel Rondeau-Millaire Avatar asked Feb 23 '16 16:02

Samuel Rondeau-Millaire


People also ask

What does MongoDB aggregation return?

aggregate() method returns a cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explain option, the document that provides details on the processing of the aggregation operation.

What is $lookup in MongoDB?

$lookup performs an equality match on the localField to the foreignField from the documents of the from collection. If an input document does not contain the localField , the $lookup treats the field as having a value of null for matching purposes.

Which aggregation method is preferred for use by MongoDB?

The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.

Can we specify more than one aggregate function simultaneously in MongoDB?

collection. aggregate () can use several channels at the same time for data processing. Db. collection.


Video Answer


2 Answers

Just to help others with this, @SiddhartAjmera has the right answer, I only needed to add double quotes for nested values like "campaign.clientid".

The final code should be:

db.somecollection.aggregate([       {         "$lookup": {           "from": "campaigns",           "localField": "campId",           "foreignField": "_id",           "as": "campaign"         }       },       {         "$unwind": "$campaign"       },       {         "$lookup": {           "from": "entities",           "localField": "campaign.clientid",           "foreignField": "_id",           "as": "campaign.client"         }       },       {         "$project": {           "_id": 1,           "campId": 1,           "articleId": 1,           "campaign._id": 1,           "campaign.clientid": 1,           "campaign.client._id": 1,           "campaign.client.username": 1         }       } ]); 
like image 172
Rodrigo Lopetegui Avatar answered Sep 28 '22 20:09

Rodrigo Lopetegui


Using pipeline and $project inside $lookup

db.somecollection.aggregate([{     $lookup: {         from: "campaigns",         localField: "campId",         foreignField: "_id",         as: "campaign"     } }, {     $unwind: "$campaign" }, {     $lookup: {         from: "entities",         let: { client_id: "$campaign.clientid" },             pipeline : [             { $match: { $expr: { $eq: [ "$_id", "$$client_id" ] } }, },             { $project : { _id:1, username:1 } }         ],         as: "campaign.client"     } }]); 
like image 38
Kaushik R Bangera Avatar answered Sep 28 '22 19:09

Kaushik R Bangera