I am trying to make lookup in MongoDB nested array. My Data is looks like.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
},
{
"date": "2022-05-02",
"shiftId": "622b55f8f59dcdd1ab9b36b1"
},
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
}
]
}
}
]
I was trying with the lookup.
{
"$lookup": {
"from": "shifts",
"localField": "shifts.shift.shiftId",
"foreignField": "_id",
"as": "shifts.shift.shiftId"
}
}
And getting the result.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
},
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"date": "2022-05-02",
"shiftId": [
{
"_id": "622b55f8f59dcdd1ab9b36b1",
"date": "2022-05-02",
"name": "Morning"
}
]
}
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
}
]
But my require data should looks like as bellow. shiftId should nested under shift array along with shifts data.
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
},
{
"date": "2022-05-02",
"shiftId": [
{
"_id": "622b55f8f59dcdd1ab9b36b1",
"date": "2022-05-02",
"name": "Morning"
}
]
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
}
]
Here is date field under shift is missing. And also need to group the shift array. Please help me out. PlayGround
Use $set after $lookup
db.employees.aggregate([
{
$lookup: {
from: "shifts",
localField: "shifts.shift.shiftId",
foreignField: "_id",
as: "shifts.shift2"
}
},
{
$set: {
"shifts.shift": {
$map: {
input: "$shifts.shift",
as: "s",
in: {
$mergeObjects: [
"$$s",
{
shiftId: {
$filter: {
input: "$shifts.shift2",
as: "s2",
cond: { $eq: [ "$$s2._id", "$$s.shiftId" ] }
}
}
}
]
}
}
}
}
},
{
$unset: [ "shifts.shift2" ]
}
])
mongoplayground
Related
return the order with the user where purchase id: 123, product id: p123 and user and order shipping.mode =2
db={
"orders": [
{
"_id": ObjectId("62155381877d4300196008ef"),
"shipping": {
"mode": 1
},
"products": [],
"user": ObjectId("6186bd3315a342001bd84f42"),
},
{
"_id": ObjectId("6215569b54cc7f0030c44e0f"),
"shipping": {
"mode": 2
},
"user": ObjectId("6186bd3315a342001bd84f43"),
"products": [
{
"id": "p123"
}
],
}
],
"users": [
{
"_id": ObjectId("6186bd3315a342001bd84f43"),
"shipping": {
"mode": 2
},
"name": "user100",
"purchase": [
{
"id": "123"
},
{
"id": "hjhh"
}
],
}
]
}
https://mongoplayground.net/p/IomR8U7Ard-
db.orders.aggregate([
{
"$lookup": {
"from": "users",
"localField": "user",
"foreignField": "_id",
"as": "user"
}
},
{
"$unwind": "$user"
},
{
"$unwind": "$user.purchase"
},
{
"$match": {
"$and": [
{
"shipping.mode": {
"$gt": 0
}
},
]
}
},
{
"$match": {
"$or": [
{
"$and": [
{
"user.shipping.mode": {
"$eq": 2
}
},
{
"user.purchase.id": {
"$eq": "123"
}
},
{
"$expr": {
"$in": [
"p123",
"$products.id"
]
}
}
]
},
]
}
},
])
I have two collections, 1. temporaryCollection, 2. permanentCollection, I would like to take data from temporaryCollection and update in permanentCollection. To see the expected result see updatedPermanentCollection below.
Fields that are taken from Temporary collection and updated in Permanent collection are:
emailAddresses
phoneNumbers
ContactName
ContactNumber
For your info, the fields that are changed in Temporary collection
contacts[0]['emailAddresses']
contacts[0]['ContactName']
contacts[0]["phoneNumbers"]
contacts[0]["ContactNumber"]
Field that are that should not be changed after updation in UpdatedPermanentCollection is
contacts._id
Note: contacts is an Array of objects, for simplicity I have shown just one object.
I am currently using the below query which updates the permanentCollection but also overrides the contacts._id field. I don't want the contacts._id field to be overridden.
Here is my MongoDB Query
db.temporaryCollection.aggregate([
{
$match: {
userID: ObjectId("61d1efea2c0fab00340f47c8"),
},
},
{
$merge: {
into: "permanentCollection",
on: "userID",
whenMatched: "merge",
whenNotMatched: "insert",
},
},
]);
1. temporaryCollection
{
"_id": { "$oid": "61d1f04266289f003452d705" },
"userID": { "$oid": "61d1efea2c0fab00340f47c8" },
"contacts": [
{
"emailAddresses": [
{ "id": "6884", "label": "email1", "email": "addedemail#gmail.com" }
],
"phoneNumbers": [
{
"label": "other",
"id": "4594",
"number": "+918984292930"
},
{
"label": "other",
"id": "4595",
"number": "+911234567890"
}
],
"_id": { "$oid": "61d1f04266289f003452d744" },
"ContactName": "Sample User 1 Name Changed",
"ContactNumber": "+918984292930",
"recordID": "833"
}
],
"userNumber": "+911234567890",
"__v": 7
}
2. permanentCollection
{
"_id": { "$oid": "61d1f04266289f003452d701" },
"userID": { "$oid": "61d1efea2c0fab00340f47c8" },
"contacts": [
{
"emailAddresses": [],
"phoneNumbers": [
{
"label": "other",
"id": "4594",
"number": "+918984292929"
},
{
"label": "other",
"id": "4595",
"number": "+911234567890"
}
],
"_id": { "$oid": "61d1f04266289f003452d722" },
"ContactName": "Sample User 1",
"ContactNumber": "+918984292929",
"recordID": "833"
}
],
"userNumber": "+911234567890",
"__v": 7
}
3. updatedPermanentCollection (Expected result)
{
"_id": { "$oid": "61d1f04266289f003452d701" },
"userID": { "$oid": "61d1efea2c0fab00340f47c8" },
"contacts": [
{
"emailAddresses": [
{ "id": "6884", "label": "email1", "email": "addedemail#gmail.com" }
],
"phoneNumbers": [
{
"label": "other",
"id": "4594",
"number": "+918984292930"
},
{
"label": "other",
"id": "4595",
"number": "+911234567890"
}
],
"_id": { "$oid": "61d1f04266289f003452d722" },
"ContactName": "Sample User 1 Name Changed",
"ContactNumber": "+918984292930",
"recordID": "833"
}
],
"userNumber": "+911234567890",
"__v": 7
}
Try with this aggregation query.
db.temporarCollection.aggreagate(
[
{
"$lookup": {
"from": "permanantCollection",
"let": {
"user_id": "$userID"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$$user_id", "$userID"
]
}
}
}
],
"as": "pcontacts"
}
}, {
"$unwind": {
"path": "$pcontacts",
"preserveNullAndEmptyArrays": true
}
}, {
"$project": {
"contacts": {
"$map": {
"input": "$contacts",
"as": "contact",
"in": {
"tcontact": "$$contact",
"pcontact": {
"$first": {
"$filter": {
"input": "$pcontacts.contacts",
"as": "pcontact",
"cond": {
"$eq": [
"$$pcontact.recordID", "$$contact.recordID"
]
}
}
}
}
}
}
},
"userNumber": 1,
"userID": 1,
"_id": 0
}
}, {
"$project": {
"contacts": {
"$map": {
"input": "$contacts",
"as": "contact",
"in": {
"emailAddresses": "$$contact.tcontact.emailAddresses",
"phoneNumbers": "$$contact.tcontact.phoneNumbers",
"ContactName": "$$contact.tcontact.ContactName",
"ContactNumber": "$$contact.tcontact.ContactNumber",
"recordID": {
"$let": {
"vars": {},
"in": {
"$cond": {
"if": "$$contact.pcontact.recordID",
"then": "$$contact.pcontact.recordID",
"else": "$$contact.tcontact.recordID"
}
}
}
},
"_id": {
"$let": {
"vars": {},
"in": {
"$cond": {
"if": "$$contact.pcontact._id",
"then": "$$contact.pcontact._id",
"else": "$$contact.tcontact._id"
}
}
}
}
}
}
},
"userNumber": 1,
"userID": 1
}
}, {
"$merge": {
"into": "pc",
"on": "userID",
"whenMatched": "replace",
"whenNotMatched": "insert"
}
}
])
It is not a fully optimized query but it works.
Try to add $unset to db query.
db.temporaryCollection.aggregate([
{
$unset: "_id"
},
{
$match: {
userID: ObjectId("61d1efea2c0fab00340f47c8"),
},
},
{
$merge: {
into: "permanentCollection",
on: "userID",
whenMatched: "merge",
whenNotMatched: "insert",
},
},
]);
I am struggling with writing an aggregation pipeline to lookup nested documents by their _id and return a specific name without overwriting the existing keys/values in the data. I have managed to do this for the nested array, but am unable to do it for an array that is nested within the nested array.
So I want to lookup the _id of each ingredient and each subIngredient and merge them with the data for these ingredients that already exists (i.e. qty, measure).
Here is what I have so far:
https://mongoplayground.net/p/ft4oIMm_8wg
Products Collection:
[
{
"_id": {
"$oid": "5ecf269bceb735416db0b329"
},
"id": 36,
"title": "Product 1",
"description": {
"generalInformation": "Some information",
"activeIngredients": [
{
"_id": 1636,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1626,
"qty": 16.6,
"measure": "µg"
}
],
},
{
"_id": 1234,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1122,
"qty": 16.6,
"measure": "µg"
},
{
"_id": 1212,
"qty": 16.6,
"measure": "µg"
}
],
},
]
},
},
{
"_id": {
"$oid": "5ecf269bceb735416db0b346"
},
"id": 36,
"title": "Product 2",
"description": {
"generalInformation": "Some information",
"activeIngredients": [
{
"_id": 1234,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1122,
"qty": 16.6,
"measure": "µg"
}
],
},
{
"_id": 1234,
"qty": 133.5,
"measure": "µg",
"subIngredient": [
{
"_id": 1122,
"qty": 16.6,
"measure": "µg"
},
{
"_id": 1212,
"qty": 16.6,
"measure": "µg"
}
],
},
]
},
}
]
Ingredients Collection:
[
{
"_id": 1234,
"name": "Ingredient 1",
},
{
"_id": 1122,
"name": "Ingredient 2",
},
{
"_id": 1212,
"name": "Ingredient 3",
},
{
"_id": 1636,
"name": "Ingredient 4",
},
{
"_id": 1626,
"name": "Ingredient 5",
}
]
What should be returned:
[
{
"_id": ObjectId("5ecf269bceb735416db0b329"),
"title": "Product 1"
"description": {
"activeIngredients": {
"_id": 1636,
"measure": "µg",
"name": "Ingredient 4",
"qty": 133.5,
"subIngredient": [
{
"_id": 1626,
"measure": "µg",
"qty": 16.6
}
]
},
"generalInformation": "Some information"
},
"ingredients": [
{
"_id": 1636,
"measure": "µg",
"name": "Ingredient 4",
"qty": 133.5,
"subIngredient": [
{
"_id": 1626,
"measure": "µg",
"qty": 16.6,
"name": "Ingredient 2"
}
]
},
{
"_id": 1234,
"measure": "µg",
"name": "Ingredient 1",
"qty": 133.5,
"subIngredient": [
{
"_id": 1122,
"measure": "µg",
"qty": 16.6,
"name": "Ingredient 2"
},
{
"_id": 1212,
"measure": "µg",
"qty": 16.6,
"name": "Ingredient 2"
}
]
}
]
},
]
My current pipeline:
[
{
"$unwind": {
"path": "$description.activeIngredients",
"preserveNullAndEmptyArrays": false
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "description.activeIngredients._id",
"foreignField": "_id",
"as": "description.activeIngredients.name"
}
},
{
"$addFields": {
"description.activeIngredients.name": {
"$arrayElemAt": [
"$description.activeIngredients.name.name",
0
]
}
}
},
{
"$group": {
"_id": "$_id",
"ingredients": {
"$push": "$description.activeIngredients"
},
"description": {
"$first": "$description"
},
"title": {
"$first": "$title"
}
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "ingredients.subIngredient._id",
"foreignField": "_id",
"as": "subIngredients"
}
}
]
Appreciate any help. Thanks.
You're not far off and you can achieve this result in multiple different ways, one of which is to just $unwind the subingredients array, $lookup on that and finally adding another $group stage to restructure the document.
As you've clearly shown you know how to do all these things i'll show a different way that utilizes operators like $map, $indexOfArray and Mongo's v3.6 $lookup syntax.
The strategy is instead of unwinding the subarray we just $lookup all the relevant sub-ingredients and then "merge" the two arrays using the operators i specified.
i.e taking:
[ {id: 5, name: "name"} ];
[ {id: 5, qty: 25} ]
And making them into:
[ {id: 5, name: "name", qty: 25} ]
Here's how we do it:
db.products.aggregate([
{
"$unwind": {
"path": "$description.activeIngredients",
"preserveNullAndEmptyArrays": false
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "description.activeIngredients._id",
"foreignField": "_id",
"as": "description.activeIngredients.name"
}
},
{
"$addFields": {
"description.activeIngredients.name": {
"$arrayElemAt": [
"$description.activeIngredients.name.name",
0
]
}
}
},
{
"$lookup": {
"from": "ingredients",
"let": {
sub: "$description.activeIngredients.subIngredient"
},
"pipeline": [
{
$match: {
$expr: {
"$setIsSubset": [
[
"$_id"
],
{
$map: {
input: "$$sub",
as: "datum",
in: "$$datum._id"
}
}
]
}
}
}
],
"as": "subIngredients"
}
},
{
"$addFields": {
"description.activeIngredients.subIngredient": {
$map: {
input: "$description.activeIngredients.subIngredient",
as: "sub",
in: {
"$mergeObjects": [
"$$sub",
{
name: {
$arrayElemAt: [
"$subIngredients.name",
{
"$indexOfArray": [
"$subIngredients._id",
"$$sub._id"
]
}
]
}
}
]
}
}
}
}
},
{
"$group": {
"_id": "$_id",
"ingredients": {
"$push": "$description.activeIngredients"
},
"description": {
"$first": "$description"
},
"title": {
"$first": "$title"
}
}
}
])
MongoPlayground
Let's consider that I have the following documents (ignoring the _id):
[
{
"Id": "Store1",
"Info": {
"Location": "Store1 Street",
"PhoneNumber": 111
},
"MaxItemsPerShelf": 3,
"Shelf": [
{
"Id": "Shelf1",
"Items": [
{
"Id": "Item1",
"Name": "bananas"
},
{
"Id": "Item2",
"Name": "apples"
},
{
"Id": "Item3",
"Name": "oranges"
}
]
},
{
"Id": "Shelf2",
"Items": [
{
"Id": "Item4",
"Name": "cookies"
},
{
"Id": "Item5",
"Name": "chocolate"
}
]
},
{
"Id": "Shelf3",
"Items": []
}
]
},
{
"Id": "Store3",
"Info": {
"Location": "Store2 Street",
"PhoneNumber": 222
},
"MaxItemsPerShelf": 2,
"Shelf": [
{
"Id": "Shelf4",
"Items": [
{
"Id": "Item6",
"Name": "champoo"
},
{
"Id": "Item7",
"Name": "toothpaste"
}
]
},
{
"Id": "Shelf5",
"Items": [
{
"Id": "Item8",
"Name": "chicken"
}
]
}
]
}
]
Given a specific Shelf.Id I want to get the following result ( Shelf.Id = "Shelf2"):
[{
"Info": {
"Location": "Store1 Street",
"PhoneNumber": 111
},
"ItemsNumber": 2,
"ItemsRemaining": 1
}]
Therefore:
ItemsNumberis the $size of Shelf
and
ItemsRemainingis equal to MaxItemsPerShelf $size of Shelf
also I want to copy the value of the Info to the aggregate output.
How can I accomplish this with aggregate? On my efforts I couldn't pass through an iterator that gets the $size of $Shelf.Items
You can use below aggregation
db.collection.aggregate([
{ "$match": { "Shelf.Id": "Shelf2" }},
{ "$replaceRoot": {
"newRoot": {
"$let": {
"vars": {
"shelf": {
"$filter": {
"input": {
"$map": {
"input": "$Shelf",
"in": {
"Id": "$$this.Id",
"count": { "$size": "$$this.Items" }
}
}
},
"as": "ss",
"cond": { "$eq": ["$$ss.Id", "Shelf2"] }
}
}
},
"in": {
"Info": "$Info",
"ItemsNumber": { "$arrayElemAt": ["$$shelf.count", 0] },
"ItemsRemaining": {
"$subtract": [
"$MaxItemsPerShelf",
{ "$ifNull": [
{ "$arrayElemAt": ["$$shelf.count", 0] },
0
]}
]
}
}
}
}
}}
])
How to populate in result of aggregated query in monogdb
Array of followedId
var followeduserId = ["abc","efg","xyz","pqr","acd","rts"];
Feeds Recommended
[
{
"feedsId": "feed1",
"userId": "abc"
},
{
"feedsId": "feed1",
"userId": "efg"
}
]
Feeds collection
[
{
"link": "www.yodo.com",
"recommended": [
"abc",
"efg"
],
"title": "This is my feed7",
"topics": [
"topi1",
"topi2",
"topi3",
"topi4"
]
},
{
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"das",
"asd",
"eqw",
"weq"
],
"title": "This is my feed8",
"topics": [
"topi1",
"topi2",
"topi3",
"topi4"
]
}
]
Ran aggregation query
feedsrecommended.aggregate([
{ $match: { userId: { $in: "followersId" }}},
{ $lookup: {
from: "feeds",
localField: "feedsId",
foreignField: "_id",
as: "feedsId"
}},
{ $group: {
"_id": { "feedsId": "$feedsId" },
"count": { "$sum": 1 }
}},
{ $sort: { count: -1 }}
])
result After aggregation
var resultfeeds = [
{
"count": 7,
"id": {
"_id": "feed1",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"xyz",
"pqr",
"acd",
"rts"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi8",
"topi6",
"topi5"
]
}
},
{
"count": 3,
"id": {
"_id": "feed5",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"acd",
"rts"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi2",
"topi3",
"topi4"
]
}
},
{
"count": 3,
"id": {
"_id": "feed6",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg",
"xyz",
"pqr"
],
"title": "This is my feed1",
"topics": [
"topi7",
"topi1",
"topi4",
"topi8"
]
}
},
{
"count": 2,
"id": {
"_id": "feed2",
"link": "www.yodo.com",
"recommended": [
"abc",
"acd",
"rts"
],
"title": "This is my feed1",
"topics": [
"topi7",
"topi6",
"topi8"
]
}
},
{
"count": 2,
"id": {
"_id": "feed7",
"link": "www.yodo.com",
"recommended": [
"abc",
"efg"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi5",
"topi6",
"topi4"
]
}
},
{
"count": 1,
"id": {
"_id": "feed3",
"link": "www.yodo.com",
"recommended": [
"abc",
"asd",
"eqw",
"weq"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi7",
"topi6",
"topi4"
]
}
},
{
"count": 1,
"id": {
"_id": "feed8",
"link": "www.yodo.com",
"recommended": [
"abc",
"das",
"asd",
"eqw",
"weq"
],
"title": "This is my feed1",
"topics": [
"topi1",
"topi2",
"topi5",
"topi4"
]
}
}
]
I want to populate topics and recommeded userName and image in the result
topic collection
[
{
"topic_name": "tiger"
},
{
"topic_name": "loin"
}
]
user collection
[
{
"name": "deepa",
"profileImg": "www.com/facebook.jpg"
},
{
"name": "nisa",
"profileImg": "www.com/facebook.jpg"
}
]
My last result should be like this
[
{
"count": 2,
"id": {
"_id": "feed2",
"link": "www.yodo.com",
"recommended": [
{
"_id": "abc",
"name": "deepa",
"profileImg": "www.com/facebook.jpg"
},
{
"_id": "acd",
"name": "sigger",
"profileImg": "www.com/facebook.jpg"
},
{
"_id": "rts",
"name": "buster",
"profileImg": "www.com/facebook.jpg"
}
],
"title": "This is my feed1",
"topics": [
{
"_id": "topi6",
"topic_name": "boolena"
},
{
"_id": "topi7",
"topic_name": "mika"
},
{
"_id": "topi8",
"topic_name": "tika"
}
]
}
}
]
You can try below aggregation in mongodb 3.6 and above
Feedsrecommended.aggregate([
{ "$match": { "userId":{ "$in": followersId }}},
{ "$group": {
"_id": "$feedsId",
"count": { "$sum": 1 }
}},
{ "$lookup": {
"from": "feeds",
"let": { "feedsId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$feedsId" ] }}},
{ "$lookup": {
"from": "topics",
"let": { "topics": "$topics" },
"pipeline": [
{ "$match": { "$expr": { "$in": [ "$_id", "$$topics" ] } } }
],
"as": "topics"
}},
{ "$lookup": {
"from": "users",
"let": { "recommended": "$recommended" },
"pipeline": [
{ "$match": { "$expr": { "$in": [ "$_id", "$$recommended" ] } } }
],
"as": "recommended"
}}
],
"as": "feedsId"
}}
])