Populate nested array objects using aggregate in mongodb - mongodb

Im trying to populate the second nested array after using aggregate $lookup.
This is my original array.
{
"_id" : ObjectId("607da9c0c7cb26384c7810a6"),
"date" : ISODate("2021-04-20T00:00:00.000+0000"),
"clientID" : "601e6dc61766587af8ce76db",
"medications" : [
{
"_id" : ObjectId("6065de3aa95e721f587f7528")
},
]
}
{
from: "medications",
localField: "medications._id",
foreignField: "_id",
as: "medications",
}
after using aggregate $lookup I get this result, but I still have nested array "inventory" that I need to populate. I tried adding another $lookup pipeline, but I don't know how the right way to do that.
{
"_id" : ObjectId("607da9c0c7cb26384c7810a6"),
"date" : ISODate("2021-04-20T00:00:00.000+0000"),
"clientID" : "601e6dc61766587af8ce76db",
"medications" : {
"_id" : ObjectId("6065de3aa95e721f587f7528"),
"medication": "med 1"
"schedule": ["9am", "10pm"]
"inventory": "ObjectId("6076d55ab6aeb947dca85877")"
}
}
This is the inventory item
{
"_id" : ObjectId("6076d55ab6aeb947dca85877"),
"item": "Inventory item 1",
"Qty": "10"
}
Expected Output
{
"_id" : ObjectId("607da9c0c7cb26384c7810a6"),
"date" : ISODate("2021-04-20T00:00:00.000+0000"),
"clientID" : "601e6dc61766587af8ce76db",
"medications" : {
"_id" : ObjectId("6065de3aa95e721f587f7528"),
"medication": "med 1"
"schedule": ["9am", "10pm"]
"inventory": {
"_id" : ObjectId("6076d55ab6aeb947dca85877"),
"item": "Inventory item 1",
"Qty": "10"
}
}
}

You can try this one or alternatively use population.
{
from: "medications",
localField: "medications._id",
foreignField: "_id",
as: "medications",
}
, {
$unwind: {
path: "$medications",
preserveNullAndEmptyArrays: true
}
}, {
$lookup: {
from: "inventory",
localField: "inventory._id",
foreignField: "_id",
as: "medications.inventory",
}
}
You may check the $unwind in the official documentation. Just pay attention to the appropriate localField and foreignField.

use populate() to expand nested references.
schemaModel.populate(result, {path: 'result'});
See more at Mongoose docs

Related

Aggregate Query returns null array

I have three collections in EmployeeDB.
Employee
{
"user_id" : "EMP001",
"FirstName" : "Manoj",
"LastName" : "sharma",
"Status" : "0/1",
"CreateDate" : "1988-10-11T18:30:00.000Z"
}
EmpContact
{
"user_id" : "EMP001",
"Phone" : "9999999999",
"Email" : "xyz#gmail.com"
}
EmpInfo
{
"user_id" : "EMP001",
"Gender" : "Male",
"Age" : NumberInt(20),
"Designation" : "manager",
"Salary" : "5000"
}
I need to group the three collections and display all fields.
I tried the below code,
db.Employee.aggregate([
{$lookup: {from: "EmpContact",localField: "user_id",foreignField: "user_id",as: "contact"}},
{$unwind:"$EmpContact"},
{$lookup: {from: "EmpInfo",localField: "user_id",foreignField: "user_id",as: "empinfo"}},
{$unwind:"$EmpInfo"},
{
$match:{user_id : "EMP001"}
}
]).toArray();
But I got an empty array, Please help me...
The problem comes from your $unwind.
Since you name your field as contact in the $lookup, the field EmpContact doesn't exist.
The same thing for the second unwind, EmpInfo doesn't exist but empinfo does
Here is the full query
db.Employee.aggregate([
{
$lookup: {
from: "EmpContact",
localField: "user_id",
foreignField: "user_id",
as: "contact"
}
},
{
$unwind: "$contact"
},
{
$lookup: {
from: "EmpContact",
localField: "user_id",
foreignField: "user_id",
as: "empinfo"
}
},
{
$unwind: "$empinfo"
},
{
$match: {
user_id: "EMP001"
}
}
])
Try it here

MongoDB Aggregation - How can I" $lookup" a nested document "_id"?

I successfully thanks to the help of the people here managed to $lookup two IDs in my document with their representive document in another collection. The next step I need to take is to further lookup a "nested" ID (refering to a document in another collection).
I tried to simply put another $lookup pipeline up but that just worked part-wise.
So it happens that an "empty" document was included into the chieftain attributes and all other attributes of chieftain where somewhat removed.
See my current aggregate:
db.getCollection('village').aggregate([
{
"$match": { _id: "111" }
},
{
"$lookup": {
from: "character",
localField: "chieftainId",
foreignField: "_id",
as: "chieftain"
}
},
{
"$lookup": {
from: "character",
localField: "villagerIds",
foreignField: "_id",
as: "villagers"
}
},
{
"$lookup": {
from: "bloodline",
localField: "chieftain.bloodline",
foreignField: "_id",
as: "chieftain.bloodline"
}
},
{ "$project" : { "villagerIds" : 0, "chieftainId" : 0}},
{ "$unwind" : "$chieftain" }
])
The result of that is the following:
{
"_id" : "111",
"name" : "MyVillage",
"reputation" : 0,
"chieftain" : {
"bloodline" : []
},
"villagers" : [
{
"_id" : "333",
"name" : "Bortan",
"age" : 21,
"bloodlineId" : "7f02191f-90af-406e-87ff-41d5b4387999",
"villageId" : "foovillage",
"professionId" : "02cbb10a-6c0f-4249-a932-3f40e12d32c5"
},
{
"_id" : "444",
"name" : "Blendi",
"age" : 21,
"bloodlineId" : "b3a8ffeb-27aa-4e2e-a8e6-b382554f326a",
"villageId" : "foovillage",
"professionId" : "45dc9350-c84a-491d-a49a-524834dd5773"
}
]
}
I expected the chieftain part to look like this (this is how the chieftain document looks like without the 'last' $lookup I added):
"chieftain" : {
"_id" : "222",
"name" : "Bolzan",
"age" : 21,
"bloodlineId" : "7c2926f9-2f20-4ccf-846a-c9966970fa9b", // this should be resolved/lookedup
"villageId" : "foovillage",
},
At the point of the lookup, chieftan is an array, so setting the chieftan.bloodline replaces the array with an object containing only the bloodline field.
Move the { "$unwind" : "$chieftain" } stage to before the bloodline lookup stage so the lookup is dealing with an object.

How can I use MongoDB's aggregate with $lookup to replace an attribute that holds an id with the whole document?

I have two collections:
user:
{
"_id" : "9efb42e5-514d-44bd-a4b8-6f74e6313ec2",
"name" : "Haralt",
"age" : 21,
"bloodlineId" : "c59a2d02-f304-49a8-a52a-44018fc15fe6",
"villageId" : "foovillage"
}
bloodlines:
{
"_id" : "c59a2d02-f304-49a8-a52a-44018fc15fe6",
"name" : "Tevla",
"legacy" : 0
}
Now I'd like to do an aggregate to replace user.bloodlineId with the whole bloodline document.
This is what I tried to far:
db.getCollection('character').aggregate([
{
"$match": { _id: "9efb42e5-514d-44bd-a4b8-6f74e6313ec2" }
},
{
"$lookup": {
from: "bloodline",
localField: "bloodlineId",
foreignField: "_id",
as: "bloodline"
}
}])
The result is almost where I want it:
{
"_id" : "9efb42e5-514d-44bd-a4b8-6f74e6313ec2",
"name" : "Haralt",
"age" : 21,
"bloodlineId" : "c59a2d02-f304-49a8-a52a-44018fc15fe6",
"villageId" : "foovillage",
"bloodline" : [
{
"_id" : "c59a2d02-f304-49a8-a52a-44018fc15fe6",
"name" : "Tevla",
"legacy" : 0
}
]
}
Only two issues here. The first is that bloodlineId is still there and bloodline was just added to the result. I'd like to have bloodline replace the bloodlineId attribute.
The second problem is that bloodline is an array. I'd love to have it a single object.
I think this pipeline might do the trick:
[
{
"$match": {
_id: "9efb42e5-514d-44bd-a4b8-6f74e6313ec2"
}
},
{
"$lookup": {
from: "bloodlines",
localField: "bloodlineId",
foreignField: "_id",
as: "bloodline"
}
},
{
$project: {
"age": 1,
"bloodlineId": {
$arrayElemAt: [
"$bloodline",
0
]
},
"name": 1,
"villageId": 1
}
}
]
Mongo Playground
If there's anything I'm missing, please let me know!

Mongodb aggregate unexpected array result - Needs to return an object from $lookup instead of an array

I'm joining two collections, but the result of trends is an array, each item always has only one trend, how do I remove the trend inside array?
'Items' collection:
{
"itemid" : "370",
"name" : "A"
},
{
"itemid" : "378",
"name" : "B"
}
'Trends' collection
{
"itemid" : "370",
"max" : "715705",
},
{
"itemid" : "378",
"max" : "35346",
}
Command executed:
db.items.aggregate([
{
$lookup: {
from: "trends",
localField: "itemid",
foreignField: "itemid",
as: "trend"
}
}
])
Result:
{
"itemid" : "370",
"name" : "A",
"trend" : [ // unexpected array, the result is always a single 'trend'
{
"itemid" : "370",
"max" : "715705",
}
]
},
...
Expected:
{
"itemid" : "370",
"name" : "A",
"trend" : { // yeah, without array
"itemid" : "370",
"max" : "715705",
}
},
...
You can do it with $unwind pipeline stage
db.items.aggregate([
{
$lookup: {
from: "trends",
localField: "itemid",
foreignField: "itemid",
as: "trend"
}
},
$unwind:"$trend"
])
It's not an unexpected result, as $lookup would result in retrieving all matched documents into an array i.e; trend will be an array in your case. It's for a reason, Assume if you're joining collection A with collection B & documents in collection A might multiple matching documents in collection B then all those documents from collection B are pulled into an array of that relative document of collection A in your case trend field. Here if you wanted to get object inside it you need to do one of these ::
db.items.aggregate([
{
$lookup: {
from: "trends",
localField: "itemid",
foreignField: "itemid",
as: "trend"
}
},
{ $addFields: { trend: {$arrayElemAt : ["$trend", 0] } } } // This stage is going re-write trend field with first object from trend array created by $lookup stage.
])
(Or)
db.items.aggregate([
{
$lookup: {
from: "trends",
localField: "itemid",
foreignField: "itemid",
as: "trend"
}
},
{ $unwind: { path: "$trend", preserveNullAndEmptyArrays: true } }
])
Note : When you use $unwind you need to be cautious that you need to use preserveNullAndEmptyArrays option as if trend is an [] - which means no match found in collection B, then $unwind is going to remove all collection A documents with trend :[] in the final aggregation result.

Aggregation when one document contains the values of multiple Documents

am trying to aggregate values of multiple documents in one, but unable to do it,
db.doc1.find().pretty();
{
"addressId" : ObjectId("5901a5f83541b7d5d3293768"),
"socialId" : ObjectId("5901b0f6d318b072ceea44fb")
}
db.doc2.find().pretty();
{
"_id" : ObjectId("5901a5f83541b7d5d3293768"),
"address" : "Gurgaon",
"mob" : "9876543211"
}
db.doc3.find().pretty();
{
"_id" : ObjectId("5901b0f6d318b072ceea44fb"),
"fbURLs" : "http://www.facebook.com",
"twitterURLs" : "http://www.twitter.com"
}
My Query
db.doc2.aggregate([
{ $match: { "_id" : ObjectId("5901a5f83541b7d5d3293768") } },
{
$lookup:
{
from: "doc1",
localField: "_id",
foreignField: "addressId",
as: "Doc2Details"
}
}
]).pretty();
db.doc3.aggregate([
{ $match: { "_id" : ObjectId("5901b0f6d318b072ceea44fb") } },
{
$lookup:
{
from: "doc1",
localField: "_id",
foreignField: "socialId",
as: "tireConfig"
}
}
]).pretty();
I want match values from Doc1 and combine values of doc2 and doc3, can someone help me with this?