Skip to content

Instantly share code, notes, and snippets.

@bhargavkonkathi
Created April 4, 2017 09:57
Show Gist options
  • Save bhargavkonkathi/47cb72a3f958fac135cb1f736384a64e to your computer and use it in GitHub Desktop.
Save bhargavkonkathi/47cb72a3f958fac135cb1f736384a64e to your computer and use it in GitHub Desktop.
{
"waitedMS" : NumberLong(0),
"result" : [
{
"_id" : ObjectId("58b80a5f922a9656b78e86a7"),
"email" : "bhargavksdn@gmail.com",
"access" : [
{
"roleId" : ObjectId("58e35fea31d05f0e95e0c8fd"),
"name" : "brgv k",
"role" : "crews",
"path" : "",
"photo" : ""
},
{
"role" : "boats"
},
{
"name" : null,
"role" : "crews"
},
{
"role" : "accounts"
},
{
"roleId" : ObjectId("580df78d3543ce1d905ffb11"),
"name" : "Nightquest",
"role" : "accounts"
},
{
"roleId" : ObjectId("58b80a62922a9656b78e86a8"),
"name" : "brgvk",
"role" : "boats",
"path" : "",
"photo" : ""
},
{
"roleId" : ObjectId("563160bb671f0c130e6f09b5"),
"name" : "Ocean Racing Club Victoria1",
"role" : "accounts"
}
]
}
],
"ok" : 1
}
@maisnamraju
Copy link

Querying multiple lookups and aggregating data into single array based on condition in mongodb

I have four collections in mongdob users, accounts, boats, crews; all with different data structures
Here are the samples

..  show one data for boats,crews, accounts and the user
.. 
..
..

Right now I am using the following aggregation pipeline and I am able to query the data

db.users.aggregate(

	// Pipeline
	[
		{
			$unwind: { "path": "$access" }
		},
		{
			$project: { "roleId": "$access.id", "email": "$email" }
		},
		{
			$lookup: {
			        "from" :"accounts",
			        "localField": "roleId",
			        "foreignField": "_id",
			        "as": "accounts"
			}
		},
		{
			$lookup: {
			        "from" :"crews",
			        "localField": "roleId",
			        "foreignField": "_id",
			        "as": "crews"
			}
		},
		{
			$lookup: {
			        "from" :"boats",
			        "localField": "roleId",
			        "foreignField": "_id",
			        "as": "boats"
			}
		},
		{
			$unwind: { 
			  "path": "$boats",
			  "preserveNullAndEmptyArrays": true
			 }
		},
		{
			$unwind: { 
			  "path": "$crews",
			  "preserveNullAndEmptyArrays": true
			 }
		},
		{
			$unwind: { 
			  "path": "$accounts",
			  "preserveNullAndEmptyArrays": true
			 }
		},
		{
			$group: { "_id": "$_id", 
			   "email" : { 
			     "$first": "$email"
			   }, 
			   "accounts": { 
			     	"$addToSet" : { 
                                 roleId:"$accounts._id",
                                 name:"$accounts.profile.accountName",
                                 "role":{$literal: 'accounts'} ,
                                 path:"$accounts.profile.path",
                                 photo:"$accounts.profile.photo"
                                 } 
			   }, 
			   "boats": {  
			     
                               "$addToSet" : { 
                                 roleId:"$boats._id",
                                 name:"$boats.name",
                                 "role":{$literal: 'boats'} ,
                                 path:"$boats.path",
                                 photo:"$boats.photo"
                                 } 
			   },
			   "crews": {  
			      "$addToSet" : { 
                                 roleId:"$crews._id",
                                 name:{ "$concat": [ "$crews.firstName", "$crews.lastName" ] }
                                 "role":{$literal: 'crews'} ,
                                 path:"$crews.path",
                                 photo:"$crews.photo"
                                 } 
			   } 
			},
                        
			
		},
        {
            "$project":{
                "_id": 1,
                 "email": 1,
                 "access": { "$setUnion": [ "$accounts", "$boats", "$crews" ]  }
              }
        }
                 
              
	]
);

Though the query works, it inserts empty fields into the access property's array 


            [ 
                {
                    "roleId" : ObjectId("58e35fea31d05f0e95e0c8fd"),
                    "name" : "brgv k",
                    "role" : "crews",
                    "path" : "",
                    "photo" : ""
                }, 
                {
                    "role" : "boats"
                }, 
                {
                    "name" : null,
                    "role" : "crews"
                }, 
                {
                    "role" : "accounts"
                }, 
                {
                    "roleId" : ObjectId("580df78d3543ce1d905ffb11"),
                    "name" : "Nightquest",
                    "role" : "accounts"
                }, 
                {
                    "roleId" : ObjectId("58b80a62922a9656b78e86a8"),
                    "name" : "brgvk",
                    "role" : "boats",
                    "path" : "",
                    "photo" : ""
                }, 
            ]

I want to remove the objects that do not have a roleId associated with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment