'Filter data based on lookup value in MongoDB
Suppose I have documents in bookUser collection as:
{
'authored_by':ObjectId('asdfs'),
'general':{'address':{'city':'XYZ'}}
}
{
'authored_by':ObjectId('jkfo21'),
'general':{'address':{'city':'YYZ'}}
}
and another collection center_data as:
{
'authored_by': ObjectId('asdfs'),
'books':{'name':'alpha'},
'book_type':'adventure'
'general':{'address':{'city':'ABC'}}
}
{
'authored_by': ObjectId('jkfo21'),
'books':{'name':'sigma'},
'book_type':'drama'
'general':{}
}
I want to fetch all the objects and get the details from the center_data collection:
Sample O/P:
[
{
'authored_by':ObjectId('asdfs'),
'general':{'address':{'city':'XYZ'}},
'books':[
{
'address': 'ABC'
}
]
},
{
'authored_by':ObjectId('jkfo21'),
'general':{'address':{'city':'YYZ'}},
'books':[
{
'address': ''
}
]
}
]
For this I tried as::
db.bookUser.aggregate([
{
$lookup: {
from: "center_data",
localField: "_id",
foreignField: "authored_by",
as: "books"
}
},
{
$project: {
books: {
$filter: {
input: "$books",
as: "book",
cond: {
$and: [
{
$eq: [
"book_type",
"adventure"
]
}
]
}
}
}
}
}
])
But on the query, it gives an empty array. I want to all the documents from bookUser collection and get the address from center_data collection and if book_type is not adventure populate address as empty.
Please let me know if anyone needs any further information.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
