MongoDB: How to populate the nested object with lookup query? - mongodb

I am fetching list of records having some nested reference to other collection, I want to populate that nested ObjectId which is inside array of objects, with mongoDb aggregate lookup query.
This is how DB collection structure is:
{
subject: {type: String},
body: {type: String},
recipients: [{
userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
stutus: {type: String, enum: ['pending','accepted','rejected'], default:'pending'}
}],
sender: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
}
What I am expecting:
[{
subject: 'Some subject here.',
body: 'Lorem ipsum dolor emit set',
recipients: [{
userId: {firstName: 'John', lastName: 'Doe'},
status: 'accepted'
},{
userId: {firstName: 'Jane', lastName: 'Doe'},
status: 'accepted'
}],
sender: {firstName: 'Jacobs', 'lastName': 'Doe'}
},{
subject: 'Some subject here.',
body: 'Lorem ipsum dolor emit set',
recipients: [{
userId: {firstName: 'Jane', lastName: 'Doe'},
status: 'rejected'
},{
userId: {firstName: 'John', lastName: 'Doe'},
status: 'accepted'
}],
sender: {firstName: 'Jacobs', 'lastName': 'Doe'}
}]
Any kind help will be greatly appreciated.

$unwind deconstruct recipients array
$lookup with users collection for recipients.userId
$unwind deconstruct recipients.userId array
$lookup with users collection for sender
$unwind deconstruct sender array
$group by _id and reconstruct recipients array
db.mails.aggregate([
{ $unwind: "$recipients" },
{
$lookup: {
from: "users",
localField: "recipients.userId",
foreignField: "_id",
as: "recipients.userId"
}
},
{ $unwind: "$recipients.userId" },
{
$lookup: {
from: "users",
localField: "sender",
foreignField: "_id",
as: "sender"
}
},
{ $unwind: "$sender" },
{
$group: {
_id: "$_id",
recipients: { $push: "$recipients" },
subject: { $first: "$subject" },
body: { $first: "$body" },
sender: { $first: "$sender" }
}
}
])
Playground

Try This:
db.emails.aggregate([
{ $unwind: "$recipients" },
{
$lookup: {
from: "users",
let: { userId: "$recipients.userId", status: "$recipients.stutus" },
pipeline: [
{
$match: {
$expr: { $eq: ["$_id", "$$userId"] }
}
},
{
$project: {
"_id": 0,
"userId": {
"firstName": "$firstName",
"lastName": "$lastName",
},
"status": "$$status"
}
}
],
as: "recipient"
}
},
{
$lookup: {
from: "users",
let: { userId: "$sender" },
pipeline: [
{
$match: {
$expr: { $eq: ["$_id", "$$userId"] }
}
},
{
$project: {
"_id": 0,
"firstName": 1,
"lastName": 1
}
}
],
as: "sender"
}
},
{
$group: {
_id: "$_id",
subject: { $first: "$subject" },
body: { $first: "$body" },
recipients: { $push: { $arrayElemAt: ["$recipient", 0] } },
sender: { $first: { $arrayElemAt: ["$sender", 0] } }
}
}
]);

Related

3 level nested lookup with arrays

I am building an apllication with mongoose and node.js that enables you to post, comment, and like both the posts and the comments.
I am tryign to build a query that gets all the information from the db.
saw that answer nested 3 level lookup like in here MongoDB nested lookup with 3 levels.
it works, but the project filter returns me an error and if I remove it, I get some comments that looks like that:
"comments":
{
"user": [],
"likes": []
}
the argigate
postArgigate = [
{
//lookup for the post likes
$lookup: {
from: 'likes',
localField: '_id',
foreignField: 'postCommentID',
as: '_likes'
},
},
//the user wrote the post
{
$lookup: {
from: 'users',
localField: 'userID',
foreignField: '_id',
as: 'user'
},
},
{
$lookup: {
from: 'comments',
localField : 'commentsID',
foreignField : '_id',
as: 'comments'
},
},
//unwind the comments to do a nesting lookup
{
$unwind: {
path: "$comments",
preserveNullAndEmptyArrays: true
}
},
//lookup in the comments likes
{
$lookup: {
from: 'likes',
localField : 'comments._id',
foreignField : 'postCommentID',
as: 'comments._likes'
},
},
//lookup in the user that wrote the comment
{
$lookup: {
from: 'users',
localField: 'comments.userID',
foreignField: '_id',
as: 'comments.user'
},
},
{
$set: {
'comments.likes':'$comments._likes.userID',
'likes': '$_likes.userID'
}
},
{
$project: {
/////////FILTER NOT WORKING////////////
// 'comments': {
// $filter: { input: "$comments", as: "cm", cond: { $ifNull: ["$$cm._id", false] } }
// } , //returns an error
'comments.userID':0,
'comments.user.password':0,
'comments._likes':0,
'userID':0,
'user.password':0,
'commentsID':0,
'_likes': 0,
}
},
{
$group: {
_id : "$_id",
user:{$first:'$user'},
likes:{$first:'$likes'},
date:{$first:'$date'},
content:{$first:'$content'},
comments: { $push: "$comments" },
}
}
]
thank you for you help!
comments
[
{
_id: ObjectId("com1"),
userID:ObjectId("eliran1"),
content: 'comment 1',
date: 2022-03-02T22:55:16.224Z,
},
{
_id:ObjectId("com2"),
userID: ObjectId("eliran1"),
content: 'comment 2',
date: 2022-03-05T18:34:52.890Z,
__v: 0
}
]
posts
[
{
_id: new ObjectId("post1"),
userID: new ObjectId("eliran1"),
content: 'post 1',
commentsID: [],
date: 2022-03-05T18:28:11.487Z,
},
{
_id: new ObjectId("post2"),
userID: new ObjectId("shira1"),
content: 'post 2',
commentsID: [ObjectId("com1"),
ObjectId("com2") ],
date: 2022-03-05T18:34:46.364Z,
}
]
users
[
{
_id: new ObjectId("eliran1"),
user: 'eliran222',
password: '123456789',
email: 'fdfd#fdfd.com33',
gender: true,
__v: 0
},
{
_id: new ObjectId("shira1"),
user: 'shira3432',
password: '123456789',
email: 'fdrf#gfge.com',
gender: false,
}
]
likes
[
{
_id: ObjectId("like1"),
userID: ObjectId("eliran1"),
postCommentID:ObjectId("post1"),
},
{
_id:ObjectId("like2"),
userID: ObjectId("shira1"),
postCommentID:ObjectId("com1"),
}
]
expected results:
[
{
_id: new ObjectId("post1"),
user: {
user: 'eliran222',
email: 'fdfd#fdfd.com33',
gender: true,
},
content: 'post 1',
comments: [],
date: 2022-03-05T18:28:11.487Z,
likes:[{/*eliran`s user*/}]
},
{
_id: new ObjectId("post2"),
userID: {
_id: new ObjectId("shira1"),
user: 'shira3432',
email: 'fdrf#gfge.com',
gender: false,
},
content: 'post 2',
comments: [{
_id: ObjectId("com1"),
user:{
_id:objectId(eliran1)
user: 'eliran222',
email: 'fdfd#fdfd.com33',
gender: true,
},
content: 'comment 1',
date: 2022-03-02T22:55:16.224Z,
likes:[{/*shira`s user*/}]
},
{
_id:ObjectId("com2"),
user: {
objectId(eliran1)
user: 'eliran222',
email: 'fdfd#fdfd.com33',
gender: true,
},
content: 'comment 2',
date: 2022-03-05T18:34:52.890Z,
likes:[]
}],
date: 2022-03-05T18:34:46.364Z,
}
]
}
]

Merge $lookup value inside objects nested in array mongoose

So I have 2 models user & form.
User Schema
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
email: {
type: String,
required: true,
}
Form Schema
approvalLog: [
{
attachments: {
type: [String],
},
by: {
type: ObjectId,
},
comment: {
type: String,
},
date: {
type: Date,
},
},
],
userId: {
type: ObjectId,
required: true,
},
... other form parameters
When returning a form, I'm trying to aggregate the user info of every user in the approvalLog into their respective objects as below.
{
...other form info
approvalLog: [
{
attachments: [],
_id: '619cc4953de8413b548f61a6',
by: '619cba9cd64af530448b6347',
comment: 'visit store for disburement',
date: '2021-11-23T10:38:13.565Z',
user: {
_id: '619cba9cd64af530448b6347',
firstName: 'admin',
lastName: 'user',
email: 'admin#mail.com',
},
},
{
attachments: [],
_id: '619cc4ec3ea3e940a42b2d01',
by: '619cbd7b3de8413b548f61a0',
comment: '',
date: '2021-11-23T10:39:40.168Z',
user: {
_id: '619cbd7b3de8413b548f61a0',
firstName: 'sam',
lastName: 'ben',
email: 'sb#mail.com',
},
},
{
attachments: [],
_id: '61a9deab8f472c52d8bac095',
by: '61a87fd93dac9b209096ed94',
comment: '',
date: '2021-12-03T09:08:59.479Z',
user: {
_id: '61a87fd93dac9b209096ed94',
firstName: 'john',
lastName: 'doe',
email: 'jd#mail.com',
},
},
],
}
My current code is
Form.aggregate([
{
$lookup: {
from: 'users',
localField: 'approvalLog.by',
foreignField: '_id',
as: 'approvedBy',
},
},
{ $addFields: { 'approvalLog.user': { $arrayElemAt: ['$approvedBy', 0] } } },
])
but it only returns the same user for all objects. How do I attach the matching user for each index?
I've also tried
Form.aggregate([
{
$lookup: {
from: 'users',
localField: 'approvalLog.by',
foreignField: '_id',
as: 'approvedBy',
},
},
{
$addFields: {
approvalLog: {
$map: {
input: { $zip: { inputs: ['$approvalLog', '$approvedBy'] } },
in: { $mergeObjects: '$$this' },
},
},
},
},
])
This adds the right user to their respective objects, but I can only add the to the root object and not a new one.
You can try the approach,
$map to iterate loop of approvalLog
$filter to iterate loop of approvedBy array and search for user id by
$arrayElemAt to get first element from above filtered result
$mergeObjects to merge current object properties of approvalLog and filtered user
$$REMOVE don't need approvedBy now
await Form.aggregate([
{
$lookup: {
from: "users",
localField: "approvalLog.by",
foreignField: "_id",
as: "approvedBy"
}
},
{
$addFields: {
approvalLog: {
$map: {
input: "$approvalLog",
as: "a",
in: {
$mergeObjects: [
"$$a",
{
user: {
$arrayElemAt: [
{
$filter: {
input: "$approvedBy",
cond: { $eq: ["$$a.by", "$$this._id"] }
}
},
0
]
}
}
]
}
}
},
approvedBy: "$$REMOVE"
}
}
])
Playground
The second approach using $unwind,
$unwind deconstruct the approvalLog array
$lookup with user collection
$addFields and $arrayElemAt to get first element from lookup result
$group by _id and reconstruct the approvalLog array and get first value of other required properties
await Form.aggregate([
{ $unwind: "$approvalLog" },
{
$lookup: {
from: "users",
localField: "approvalLog.by",
foreignField: "_id",
as: "approvalLog.user"
}
},
{
$addFields: {
"approvalLog.user": {
$arrayElemAt: ["$approvalLog.user", 0]
}
}
},
{
$group: {
_id: "$_id",
approvalLog: { $push: "$approvalLog" },
userId: { $first: "$userId" },
// add your other properties like userId
}
}
])
Playground

MongoDb: aggregation $lookup

Given following collections.
I have to get the title field and combine to identifier
CREDENTIAL:
{
_id: ..
title: ..
}
USER_CREDENTIAL:
{
_id: ..
credential_id: .. (from credential collection)
created_at: ..
identifier: {
first_name: ..
middle_name: ..
last_name: ..
}
}
The response should be:
{
user_credential_id:
member: {
first_name:
middle_name:
last_name:
title:
created_at:
}
}
$lookup - Join user and user_credential_id with a pipeline to match the condition ($match) and decorate the document ($project).
$unwind - Deconstruct member array to multiple documents.
$project - Decorate the output document.
db.user.aggregate([
{
"$lookup": {
"from": "user_credential",
let: {
"user_credential_id": "$_id",
"title": "$title"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$credential_id",
"$$user_credential_id"
]
}
}
},
{
$project: {
first_name: "$identifier.first_name",
middle_name: "$identifier.middle_name",
last_name: "$identifier.last_name",
title: "$$title",
created_at: "$created_at",
}
}
],
"as": "member"
}
},
{
$unwind: "$member"
},
{
$project: {
_id: 0,
user_credential_id: "$_id",
member: 1
}
}
])
Sample Mongo Playground
You can try this query
db.user.aggregate([
{
$lookup: {
from: "credentials",
localField: "credential_id",
foreignField: "_id",
as: "user_credential"
}
},
{
$unwind: "$user_credential"
},
{
$project: {
_id: 0,
user_credential_id: "$credential_id",
member: {
first_name: "$identifier.first_name",
middle_name: "$identifier.middle_name",
last_name: "$identifier.last_name",
title: "$user_credential.title",
created_at: "$created_at",
}
}
}
])
You can check it out here

Getting `[ [Object] ]` as nested array of object response on MongoDB Aggregation query

I'm trying to do an aggregation on two collections that has a linkage between them, and I need to access information in an array of objects in one of those collections.
Here are the schemas:
User Schema:
{
_id: ObjectId,
username: String,
password: String,
associatedEvents: [
{
event_id: ObjectId,
isCreator: boolean,
access_level: String,
}
]
}
Event Schema:
{
_id: ObjectId,
title: String,
associated_users: [
{
user_id: ObjectId
}
]
}
I'm attempting to get the users associated to an event for a specific user, and then get their access level information. Here's the aggregation I have:
const eventsJoined = await Event.aggregate([
{
$match: {
$expr: { $in: [id, "$associatedUserIds"] },
},
},
{
$lookup: {
from: "users",
localField: "associatedUserIds",
foreignField: "_id",
as: "user_info",
},
},
{ $unwind: "$user_info" },
{
$unwind: {
path: "$user_info.associatedEvents",
preserveNullAndEmptyArrays: true,
},
},
{
$group: {
_id: "$_id",
title: { $first: "$title" },
description: { $first: "$description" },
startDate: { $first: "$startdate" },
userInfo: { $first: "$user_info" },
usersAssociatedEvents: { $push: "$user_info.associatedEvents" },
},
},
{
$project: {
title: 1,
description: 1,
startDate: 1,
userInfo: 1,
usersAssociatedEvents: "$usersAssociatedEvents",
},
},
]);
And this is the result I'm getting:
[
{
_id: 609d5ad1ef4cdbeb32987739,
title: 'hello',
description: 'desc',
startDate: null,
usersAssociatedEvents: [ [Object] ]
}
]
As you can see, the query is already aggregating the correct data. But the last thing that's tripping me up is the fact that the aggregation is [ [Object] ] for usersAssociatedEvents instead of the actual contents of the object. Any idea on why that would be?

multiple $lookup stages in mongodb aggregation

I'm working with the followings mongoose schemas:
Question schema:
var Question = new Schema({
title: String,
content: String,
createdBy: {
type: Schema.ObjectId,
ref: 'User',
required: true
},
answers: {
type: [ { type: Schema.Types.ObjectId, ref: 'Answer' } ]
}
});
Answer Shchema:
var Answer = new Schema({
content: {
type: String,
require: 'Content cannot be empty.'
},
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},
isBest: {
type: Boolean,
default: false
},
createdAt: {
type: Date,
default: Date.now
},
votes: {
type: Number,
default: 0
},
comments: {
type: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
}
});
and Comment Schema:
var Comment = new Schema({
content: {
type: String,
required: [true, 'Content cannot be empty']
},
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},
createdAt: {
type: Date,
default: Date.now
}
});
Basically what I'm trying to do is doing a $lookup for answers and for comments array in every answer, and then in $project stage try to add an isOwner field that is going to be true if the user logged is the owner of the answer or comment. This is what I' trying:
Question.aggregate([
{
$match: { '_id': { $eq: questionId } }
},
{
$lookup: {
from: 'answers',
localField: 'answers',
foreignField: '_id',
as: 'answers'
}
},{
$lookup:{
from: 'comments',
localField: 'answers.comments',
foreignField: '_id',
as: 'comments'
}
}, {
$project: {
title: true,
content: true,
createdBy: true,
createdAt: true,
isOwner: { $eq : ['$createdBy', currentUser] },
answers: true,
answers: {
isOwner: { $eq : ['$createdBy', currentUser] },
content: true,
createdBy: true,
createdAt: true,
comments: {
content: true,
createdAt: true,
createdBy: true,
isOwner: { $eq : ['$createdBy', currentUser] }
}
}
}
}
])
This is the ouput that I'm expecting:
{
"_id": "58a7be2c98a28f18acaa4be4",
"title": "Some title",
"createdAt:": "2017-03-03T05:13:41.061Z",
"content": "some content",
"isOwner": true,
"createdBy": {
"_id": "58a3a66c088fe517b42775c9",
"name": "User name",
"image": "imgUrl"
},
"answers": [
{
"_id": "58a3a66c088fe517b42775c9",
"content": "an answer content",
"createdAt": "2017-03-03T05:13:41.061Z",
"isBest": false,
"isOwner": false,
"createdBy":{
"_id": "58a3a66c088fe517b42775c9",
"name": "another user",
"image": "another image"
},
"comments": [
{
"_id": "58aa104a4254221580832a8f",
"content": "some comment content",
"createdBy": {
"_id": "58a3a66c088fe517b42775c9",
"name": "another user",
"image": "another image"
},
}
]
}
]
}
I'm using mongodb 3.4.2
You can try addFields stage to add the isOwner field for all the relations.
Question.aggregate([{
$match: {
'_id': {
$eq: questionId
}
}
}, {
$addFields: {
"isOwner": {
$eq: ['$createdBy', currentUser]
}
}
}, { // No unwind needed as answers is scalar of array values.
$lookup: {
from: 'answers',
localField: 'answers',
foreignField: '_id',
as: 'answers'
}
}, {
$addFields: {
"answers.isOwner": {
$eq: ['$createdBy', currentUser]
}
}
}, {
$unwind: "$answers" //Need unwind here as comments is array of scalar array values
}, {
$lookup: {
from: 'comments',
localField: 'answers.comments',
foreignField: '_id',
as: 'comments'
}
}, {
$addFields: {
"comments.isOwner": {
$eq: ['$createdBy', currentUser]
}
}
}, {
$addFields: { // Move the comments back to answers document
"answers.comments": "$comments"
}
}, {
$project: { // Remove the comments embedded array.
"comments": 0
}
}, {
$group: {
_id: null,
isOwner: {
$first: "$isOwner"
},
answers: {
$push: "$answers"
}
}
}])
THe problem with your code is that you have not unwind the answeeres array before lookup
Please check below comment
Question.aggregate([
{
$match: { '_id': { $eq: questionId } }
},
{
$lookup: {
from: 'answers',
localField: 'answers',
foreignField: '_id',
as: 'answers'
}
},
{$unwind : "$answers"}, // <-- Check here
{
$lookup:{
from: 'comments',
localField: 'answers.comments',
foreignField: '_id',
as: 'comments'
}
},
{
$group : {
_id : null,
title: {$first : true},
content: {$first :true},
createdBy: {$first :true},
createdAt: {$first :true},
isOwner: { $eq : ['$createdBy', currentUser] },
answersStatus: {$first :true},
answers : {$push : $answer}
}
}
])