MongoDB $lookup on array of object - mongodb

I have 2 collections structured as below. I have tried $lookup to get the result but I am not getting any result because of my local and foreign fields are in array of object.
Below is my structure:
{
"_id" : ObjectId("5795a3531d3f3afc19caefef"),
"name" : "category1",
"updatedAt" : "1469431592786",
"resources" : [
{
"_id" : ObjectId("5791be003fa3bedc15d3adde"),
"title" : "resource1",
"availability" : false
},
{
"_id" : ObjectId("5795a3771d3f3afc19caeff0"),
"title" : "resource2",
"availability" : true
}
]
}
Above "categories" schema have resources array of object. this resource _id is stored in bookings collection in following way:
"booking":
{
"_id" : ObjectId("57960aa8000ca7a46b7ef683"),
"name" : "1469491200000",
"__v" : 0,
"schedule" : [
{
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep",
"_id" : ObjectId("57960aa8f9f9951c1fc923b1")
},
{
"resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "mr3",
"_id" : ObjectId("57960aa8f9f9951c1fc923b2")
},
{
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep23",
"_id" : ObjectId("57960aa8f9f9951c1fc923b3")
}
]
}
Now I want to get all the schedule of booking collection with their resource information.I want to fetch resources from categories table on the basis of booking schedule.
Desired output:
[
{
"name" : "1469491200000",
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"resourceTitle":"title",
"availability":false,
"bookings": [
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep",
"_id" : ObjectId("57960aa8f9f9951c1fc923b1")
},
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep23",
"_id" : ObjectId("57960aa8f9f9951c1fc923b3")
}
]
},
{
"name" : "1469491200000",
"resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
"resourceTitle":"mr3",
"availability":false,
"bookings": [
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "mr3",
"_id" : ObjectId("57960aa8f9f9951c1fc923b2")
}
]
}
]
Help me to get this desired result.
Thanks.

Related

$lookup aggregation failed to display array value

I'm working with MongoDB and I've 2 collections.
data collection ["user-profile"]
{
"_id" : ObjectId("60650e6fc4b4603e1e78bb23"),
"firstName" : "Luthfan",
"lastName" : "Difiesa",
"mobile" : "86742633497",
"gender" : "male",
"createdOn" : ISODate("2021-04-22T05:26:07.428+0000"),
"updatedOn" : ISODate("2021-04-22T05:26:55.218+0000")
}
data collection ["user-wishlist"]
{
"_id" : ObjectId("60650e7a1a4a817a1dd0a29c"),
"userId" : "86742633497",
"contents" : [
{
"_id" : ObjectId("5ef9d2da228f840bbd41649c"),
"name" : "Kelas 11"
}
]
}
expected output:
{
"_id" : ObjectId("60650e6fc4b4603e1e78bb23"),
"firstName" : "Luthfan",
"lastName" : "Difiesa",
"mobile" : "86742633497",
"gender" : "male",
"contents" : [
{
"_id" : ObjectId("5ef9d2da228f840bbd41649c"),
"name" : "Kelas 11"
}
]
}
Here's the query:
db.getCollection("user-profile").aggregate(
[
{
"$lookup" : {
"from" : "user-wishlist",
"localField" : "mobile",
"foreignField" : "userId",
"as" : "contents"
}
}
],
{
"allowDiskUse" : false
}
);
But the result is like this:
{
"_id" : ObjectId("60650e6fc4b4603e1e78bb23"),
"firstName" : "Luthfan",
"lastName" : "Difiesa",
"mobile" : "86742633497",
"gender" : "male",
"contents" : [
]
}
is't because of collection name using special character or type data from foreign or localField? thank u...
Your query looks good, just need add a stage after lookup to achieve your desire result,
{
$addFields: {
contents: {
$arrayElemAt: ["$contents.contents", 0]
}
}
}
Playground

I Want add if data in element array not exists

I have this data in database.
{
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b"),
"name" : "Jhones Crows",
"hobbies" : [
{
"name" : "swim",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b")
},
{
"name" : "run",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7c")
}
]
}
And I try to add data into hobbies if data in hobbies not exist. I try this :
db.getCollection('milo').update(
{'_id' : ObjectId("5a6ef287370ff5dc3d6fda7b"), 'hobbies.name' : 'sport'},
{ $addToSet : { 'hobbies' : {
'name' : 'sport',
}}
},
{upsert : true}
)
And I want data result like this :
{
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b"),
"name" : "Jhones Crows",
"hobbies" : [
{
"name" : "swim",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7b")
},
{
"name" : "run",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7c")
},
{
"name" : "sport",
"_id" : ObjectId("5a6ef287370ff5dc3d6fda7a")
}
]
}
so suppose the value of sport is not in hoobies.name. will add a new name object in the hobbies. but if there is not change anything

A query to return a single attribute of a document in mongodb

[ "_id" : ObjectId("59d1f4d0539f772aacf90f61"),
"user_id" : 1222.0,
"user_name" : "jadenn",
"email" : "jdkdkdjjjjjjjjjjjjjjjjjjjh#j.com",
"phone" : 333333333.0,
"image_ref" : "static/image/123IMG_20170917_232813.jpg",
"stories" : [
{
"title" : "accident",
"description" : "kjsbskns",
"longitude" : 19.2,
"latitude" : 81.4,
"location" : "isl",
"date" : 12.0,
"reactions" : [
{
"type" : "seen",
"date" : "1234",
"user_id" : 123.0
},
{
"type" : "seen",
"date" : "1234",
"user_id" : 123.0
}
],
"comments" : [
{
"comment" : "djksnjknfkjguye",
"date" : 123.0,
"user_id" : 123.0
},
{
"comment" : "this is the accident case ",
"date" : 123.0,
"user_id" : 123.0
}
],
"reports" : [
{
"description" : "uye",
"date" : 123.0,
"user_id" : 1232321.0
}
],
"catagory_name" : [
{
"type" : "uye"
}
]
}
]
}
Here is my document I want a query which will return only list of stories. Not a complete document of user. I searched but could not find the solution. Every solution return complete document by applying "Where condition as MYSQL"]1
You need to project on stories key from your document as below:
db.youCollection.find({}, {stories:1, _id:0})

Updating two level sub-document which is list in mongoDB?

I have a hotel collection whose one of the document looks like this -
{
"_id" : "HOTEL_1",
"name" : "Decent hotel",
"chainId" : "CHN123",
"rooms" : [
{
"id" : "ROM1",
"name" : "decent rooms",
"ratePlans" : [
{
"ratePlanId" : "RPNB1191989873C2G",
"status" : "INACTIVE",
"marginPart" : {
"marginType" : "PERCENTAGE",
"margin" : "32"
}
},
{
"ratePlanId" : "RPNE0992HBG6I0GE8",
"status" : "INACTIVE",
"marginPart" : {
"marginType" : "PERCENTAGE",
"margin" : "32"
}
}
]
},
{
"id" : "ROM2",
"name" : "another decent rooms"
"ratePlans" : []
}
]
}
I need to update status as ACTIVE of all the rate plans of all the rooms with a certain condition like chainId.
I tried with this but failed -
db.hotel.updateMany({ "chainId" : "CHN_123"},{$set : {"rooms.$ratePlans.$status" : "ACTIVE" }});
I also want to update margin as common value say 50% to all such rates.
Instead of updateMany try the below query
db.hotel.update({ "chainId" : "CHN_123"},{$set : {"rooms.$ratePlans.$status" : "ACTIVE" }},{multi:true,upsert:false},function(err,doc){
console.log(doc)
});
It works always!!

Get document based on multiple criteria of embedded collection

I have the following document, I need to search for multiple items from the embedded collection"items".
Here's an example of a single SKU
db.sku.findOne()
{
"_id" : NumberLong(1192),
"description" : "Uploaded via CSV",
"items" : [
{
"_id" : NumberLong(2),
"category" : DBRef("category", NumberLong(1)),
"description" : "840 tag visual",
"name" : "840 Visual Mini Round",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(7),
"category" : DBRef("category", NumberLong(2)),
"description" : "Maxi",
"name" : "Maxi",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(11),
"category" : DBRef("category", NumberLong(3)),
"description" : "Button",
"name" : "Button",
"version" : NumberLong(0)
},
{
"_id" : NumberLong(16),
"category" : DBRef("category", NumberLong(4)),
"customizationFields" : [
{
"_class" : "CustomizationField",
"_id" : NumberLong(1),
"displayText" : "Custom Print 1",
"fieldName" : "customPrint1",
"listOrder" : 1,
"maxInputLength" : 12,
"required" : false,
"version" : NumberLong(0)
},
{
"_class" : "CustomizationField",
"_id" : NumberLong(2),
"displayText" : "Custom Print 2",
"fieldName" : "customPrint2",
"listOrder" : 2,
"maxInputLength" : 17,
"required" : false,
"version" : NumberLong(0)
}
],
"description" : "2 custom lines of farm print",
"name" : "Custom 2",
"version" : NumberLong(2)
},
{
"_id" : NumberLong(20),
"category" : DBRef("category", NumberLong(5)),
"description" : "Color Red",
"name" : "Red",
"version" : NumberLong(0)
}
],
"skuCode" : "NF-USDA-XC2/SM-BC-R",
"version" : 0,
"webCowOptions" : "840miniwithcust2"
}
There are repeat items.id throughout the embedded collection. Each Sku is made up of multiple items, all combinations are unique, but one item will be part of many Skus.
I'm struggling with the query structure to get what I'm looking for.
Here are a few things I have tried:
db.sku.find({'items._id':2},{'items._id':7})
That one only returns items with the id of 7
db.sku.find({items:{$all:[{_id:5}]}})
That one doesn't return anything, but it came up when looking for solutions. I found about it in the MongoDB manual
Here's an example of a expected result:
sku:{ "_id" : NumberLong(1013),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(12) },
{ "_id" : NumberLong(16) },
{ "_id" :NumberLong(2) } ] },
sku:
{ "_id" : NumberLong(1014),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(2) },
{ "_id" : NumberLong(16) },
{ "_id" :NumberLong(24) } ] },
sku:
{ "_id" : NumberLong(1015),
"items" : [ { "_id" : NumberLong(5) },
{ "_id" : NumberLong(7) },
{ "_id" : NumberLong(12) },
{ "_id" : NumberLong(2) },
{ "_id" :NumberLong(5) } ] }
Each Sku that comes back has both a item of id:7, and id:2, with any other items they have.
To further clarify, my purpose is to determine how many remaining combinations exist after entering the first couple of items.
Basically a customer will start specifying items, and we'll weed it down to the remaining valid combinations. So Sku.items[0].id=5 can only be combined with items[1].id=7 or items[1].id=10 …. Then items[1].id=7 can only be combined with items[2].id=20 … and so forth
The goal was to simplify my rules for purchase, and drive it all from the Sku codes. I don't know if I dug a deeper hole instead.
Thank you,
On the part of extracting the sku with item IDs 2 and 7, when I recall correctly, you have to use $elemMatch:
db.sku.find({'items' :{ '$all' :[{ '$elemMatch':{ '_id' : 2 }},{'$elemMatch': { '_id' : 7 }}]}} )
which selects all sku where there is each an item with _id 2 and 7.
You can use aggregation pipelines
db.sku.aggregate([
{"$unwind": "$sku.items"},
{"$group": {"_id": "$_id", "items": {"$addToSet":{"_id": "$items._id"}}}},
{"$match": {"items._id": {$all:[2,7]}}}
])