Get results in nested objects - mongodb

This is my collection structure and I want to filter all the results for a defined reference:
{
"_id" : "5xFusfnvRobfMhRKE",
"book" : "Lorem",
"publisher" : "Lorem",
"author" : "Lorem",
"edition" : [
{
"edition" : "Lorem",
"year" : 2015,
"section" : [
{
"pageNumbers" : "12",
"reference" : "4NoHjACkjHJ8mavv9"
}
]
}
]
}
My attempt was Collection.find({'edition.section.reference': '4NoHjACkjHJ8mavv9'}), but that doesn't work. I would expect this matches the above example.

I think this query can help you-
db.collection.find({"edition.section.reference":"4NoHjACkjHJ8mavv9"},{}).pretty()

You have to use 'db' before the collection_name ie. db.collection.find() and for your problem you actually did it right but just missed out on 'db'.
db.sys_test.insert({"-_id":10,"edition":[{"section":[{"reference":"101"}]}]})
db.sys_test.find({"edition.section.reference":"101"}).pretty()
{
"_id" : ObjectId("55faba519d0ff7079e6f9817"),
"-_id" : 10,
"edition" : [
{
"section" : [
{
"reference" : "101"
}
]
}
]
}

Related

MongoDB query for documents with nested objects and array

I am having trouble with mongodb querying my object document.
I have the following document:
{
"_id" : ObjectId("1"),
"name" : "Bob",
"fields" : {
"DATE" : [
{
"fromDate" : null,
"values" : [
"2022-08-01"
]
}
]
},
{
"_id" : ObjectId("2"),
"name" : "John",
"fields" : {
"DATE" : [
{
"fromDate" : null,
"values" : [
"1901-08-01"
]
}
]
}
I am trying to get the document where fields.date[0].values is equal to 2022-08-01.
How can I achieve that? Thank you.
You are looking for a nested element value which having some specific value/date.
Since your document is incomplete so, I am creating one for illustrating the solution.
db.employees.insertMany([{
"name" : "Bob",
"fields" : {
"DATE" : [
{
"fromDate" : null,
"values" : [
"2022-08-01"
]
}
]
}},
{
"name" : "John",
"fields" : {
"DATE" : [
{
"fromDate" : null,
"values" : [
"1901-08-01"
]
}
]
}
},
{
"name" : "Eve",
"fields" : {
"DATE" : [
{
"fromDate" : null,
"values" : [
"2022-08-01"
]
}
]
}},
]);
Now we can find the specific value (2022-08-01 as per your requirement) from the nested array/element inside the document. Since the value resides inside "DATE" which is inside the "fields". So we can get that value easily by calling "fields.DATE".
For example you can write some code like this :
db.employees.find({"fields.DATE":{"$elemMatch": {"values": "2022-08-01"}}})
From the above code you will get a result like this.
{ "_id" : ObjectId("62e94392389644cb3fc9c81f"), "name" : "Bob", "fields" : { "DATE" : [ { "fromDate" : null, "values" : [ "2022-08-01" ] } ] } }
{ "_id" : ObjectId("62e94392389644cb3fc9c821"), "name" : "Eve", "fields" : { "DATE" : [ { "fromDate" : null, "values" : [ "2022-08-01" ] } ] } }
Hope this will help you to solve this issue. Happy coding:)

MongoDB Cross-Collection Query

Assuming the following structure:
Assets
{
"_id" : LUUID("d34a3fed"),
"name" : "A",
"records" : [
LUUID("3627f3ac"),
LUUID("80e9d125"),
LUUID("4d5e8af5"),
LUUID("17593a39"),
}
Records
{
"_id" : LUUID("3627f3ac"),
"Fields" : [
{
"Name" : "foo",
"Value" : "bar",
}
],
}
My goal is to use a find() or aggregate() to cross-reference the two collections above. The two collections share the LUUID values.
{"records": "LUUID("3627f3ac")"}
{"_id": "LUUID("3627f3ac")"}
Ultimately retrieving the:
{"Fields.Name": "foo"}
name of the Records collection
Maybe something like this:
mongos> db.records.find()
{ "_id" : ObjectId("5ff8ccf9e0f1b975b90d7a86"), "fields" : [ { "name" : "foo", "value" : "bar" } ] }
{ "_id" : ObjectId("5ff8ccf9e0f1b975b90d7a87"), "fields" : [ { "name" : "foo2", "value" : "bar2" } ] }
{ "_id" : ObjectId("5ff8ccf9e0f1b975b90d7a88"), "fields" : [ { "name" : "foo3", "value" : "bar3" } ] }
mongos> db.assest.find()
{ "_id" : ObjectId("5ff8cd72e0f1b975b90d7a87"), "name" : "A", "records" : [ ObjectId("5ff8ccf9e0f1b975b90d7a86"), ObjectId("5ff8ccf9e0f1b975b90d7a87") ] }
mongos> db.assest.aggregate([ { $lookup:{ from:"records" , localField:"records" , foreignField:"_id" , as:"match" } } , {$unwind:"$match"} , {$unwind:"$match.fields"} ,{$project:{ "Fields_name":"$match.fields.name" ,_id:0}} ])
{ "Fields_name" : "foo" }
{ "Fields_name" : "foo2" }
mongos>
Playground

updating the nested arrays in mongodb

I know that We can update only 1 level down. But can someone tell me, one level down it means that I can update only _id, name, users, lists? or list/cards ???
I know that I can use dot notation like lists.1.list, but can I make dot notation with use variable req.body.number ? for example lists.reqbody.number.lists ? I tried did not work, maybe I do something wrong. Can someone help me?
{
"_id" : ObjectId("59e096a7622a3825ac24f343"),
"name" : "1",
"users" : [
ObjectId("59cd114cea98d9326ca1c421")
],
"lists" : [
{
"list" : "1",
"cards" : [
{
"name" : "2",
"Author" : [
"59df60fb6fad6224f4f9f22d",
"59df60fb6fad6224f4f9f22d",
"59df60fb6fad6224f4f9f22e"
]
},
{
"name" : "3",
"Author" : []
}
]
},
{
"list" : "fea",
"cards" : [
{
"name" : "card",
"Author" : []
}
]
}
],
"__v" : 0 }

I need help in find object by id in array nested in object

school object :{
"id":"school1"
"adress":"aaaaa"
"studentInfo":{
"details" : [
{
"class" : "a",
"studentId" : "dan",
"subject" : "math",
},
{
"class" : "b",
"studentId" : "ron",
"subject" : "math",
}
]
}
}
I having a problem to get specific object from details array.
my desired result is to find by studentId:
{
"class" : "a",
"studentId" : "dan",
"subject" : "math",
}
I tried :
db.getCollection('schools').find(
{'name':'school1'},
{'studentInfo.details':{$elemMatch:{'studentId':'dan'} } }
)
Thanks in advance
this is one way of doing it : below is the shell command:
db.schools.find({"id" : "school1", "studentInfo.details.studentId" :"dan"}, {"studentInfo.details.$" : 1})
db.findOne({ studentId: "dan" })

add new item value into an embedded document in mongoDB

I have a document :
{
"_id" : ObjectId("550c00f81bcc15211016699b"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2"
}
]
}
All I want to do is add another item in testa like:
{
"_id" : ObjectId("550c00f81bcc15211016699b"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2",
"item3 : "item3"
}
]
}
I tried using
db.books.update(
{ "author":"mno" },
{ $addToSet: { testa : {"item3":"item3"} } }
)
this gives
{
"_id" : ObjectId("550c05261bcc15211016699c"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2"
},
{
"item3" : "item3"
}
]
}
and i tried
db.books.update(
{ "author":"mno" , "testa.item1" : "item1"},
{ $set : {"testa.0" : {"item3":"item3"}}},
{upsert : true / false}
)
this gave
{
"_id" : ObjectId("550c05fa1bcc15211016699d"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item3" : "item3"
}
]
}
Am I doing something wrong, I checked everywhere
Insert an embedded document to a new field in mongodb document
and
Update or replace an embedded document in MongoDB collection
I tried wierd things... but.... Please help me get the query right
I also tried these using the C# driver
like
WriteConcernResult res = booksColl.Update(query, Update.Set("testa.$.item1", "itemedited"),UpdateFlags.Upsert);
The following statement should work
db.books.update(
{ "author":"mno" },
{ $set: { "testa.0.item3" : "item3"} }
)
You can use the dot notation to specify items in the array and then you can just set the new item3 field to what ever value you wish. You nearly had it right in your examples above - you just need to specify "testa.0.item3" instead of "testa.0"
I would agree with #chridam's comment above though and changing the schema would make the document easier to work with going forward. It might be some extra work now but it will save you in the long run.
Actually you don't have a collection of items, you have a collections of documents which have items as keys:values, if this is the goal, the update may be:
> db.books.update({ "author":"mno" },{$set: {"testa.0.item3":"item3"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.books.find().pretty()
{
"_id" : ObjectId("550c00f81bcc15211016699b"),
"name" : "book3",
"author" : "mno",
"publisher" : "pub",
"testa" : [
{
"item1" : "item1",
"item2" : "item2",
"item3" : "item3"
}
]
}