updating the nested arrays in mongodb - 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 }

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:)

Get results in nested objects

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"
}
]
}
]
}

need to find a way to get data inside subdocument in mongo

I need a way to find inside nested array documents.
I want to find value matching inside street_1.
this is my query:
db.phonebook.find({'address.home.street_1' : 'street 1 result'});
and my document:
{
"_id" : ObjectId("53788c0c74d3ead0098b4568"),
"first_name" : "jarod",
"last_name" : "petters",
"company" : "nostromos",
"phone_numbers" : [
{
"cell" : "0752203337"
},
{
"home" : "0850819201"
},
{
"home" : "0499955550"
}
],
"website" : "http://www.mywebsite.com",
"email" : [
{
"home" : "email.first.com"
},
{
"office" : "email.second.com"
}
],
"address" : [
{
"home" : {
"stree_1" : "street 1 result",
"stree_2" : "",
"postal_code" : "66502",
"city" : "my littre city",
"country" : "usa"
}
}
],
"nationality" : "mars",
"date_of_birth" : "1978-01-01"
}
Your query is the good one. But your document is not good, you have done a mistake in your document, you write stree_1 instead of street_1. If your document is right, change your query to :
db.phonebook.find({'address.home.stree_1' : 'street 1 result'});

MongoDB Update $push Cannot apply the positional operator without a corresponding query field containing an array

model:
{
"_id" : "a62107e10f388c90a3eb2d7634357c8b",
"_appid" : [
{
"_id" : "1815aaa7f581c838",
"events" : [
{
"_id" : "_TB_launch",
"boday" : [
{
"VERSIONSCODE" : "17",
"NETWORK" : "cmwap",
"VERSIONSNAME" : "2.4.0",
"IMSI" : "460026319223205",
"PACKAGENAME" : "com.androidbox.astjxmjmmshareMM",
"CHANNELID" : "xmjmm17",
"CHANNELNAME" : "浠..?.M寰.俊?.韩?.?1.x锛.,
"eventid" : "_TB_launch",
"uuid" : "a62107e10f388c90a3eb2d7634357c8b",
"creattime" : "1366300799766",
"ts" : ISODate("2013-04-25T06:28:36.403Z")
}
],
"size" : 1
}
],
"size" : 1
}
],
"size" : 1
}
> db.events.update(
{
"_id":"039e569770cec5ff3811e7410233ed27",
"_appid._id":"e880db04064b03bc534575c7f831a83a",
"_appid.events._id":"_TB_launch"
},
{
"$push":{
"_appid.$.events.$.boday":{"111":"123123"}
}
}
);
Cannot apply the positional operator without a corresponding query field containing an array.
Why?!!
You are trying to reference multiple levels of embedding - you can only have one positional $ operator. You won't be able to do something like this until this feature request has been implemented.
Response Here
The short answer is, "no", but working with nested arrays gets
tricky. Here is an example:
db.foo.save({_id: 1, a1:[{_a1id:1, a2:[{a2id:1, a3:[{a3id:1, a4:"data"}]}]}]})
db.foo.find()
{ "_id" : 1, "a1" : [
{ "_a1id" : 1, "a2" : [
{ "a2id" : 1, "a3" : [
{ "a3id" : 1, "a4" : "data" }
] }
] }
] }
db.foo.update({_id:1}, {$push:{"a1.0.a2.0.a3":{a3id:2, a4:"other data"}}})
db.foo.find()
{ "_id" : 1, "a1" : [
{ "_a1id" : 1, "a2" : [
{ "a2id" : 1, "a3" : [
{ "a3id" : 1, "a4" : "data" },
{ "a3id" : 2, "a4" : "other data" }
] }
] }
] }
If you are unsure where one of your sub-documents lies within an
array, you may use one positional operator, and Mongo will update the
first sub-document which matches. For example:
db.foo.update({_id:1, "a1.a2.a2id":1}, {$push:{"a1.0.a2.$.a3":{a3id:2, a4:"other data"}}})

How do I remove the "value" in mapReduce?

I was wondering if it is possible to remove the "value" key in the map-reduce so that the final result just contains the values directly rather than being inside the "value" key. I am looking to do it with just the commands (so no Javascript variables and such)
For example, the map-reduce output is typically
[
{
"_id" : 0,
"value" : {
"name" : "Apple",
"sold" : 1234
}
},
{
"_id" : 1,
"value" : {
"name" : "Amazon",
"sold" : 5678
}
}
]
I would like it to end up as
[
{
"_id" : 0,
"name" : "Apple",
"sold" : 1234
},
{
"_id" : 1,
"name" : "Amazon",
"sold" : 5678
}
]
I am thinking it can be done with the findAndModify command but I am not exactly sure how.
It does not seems to be possible for now. There is a JIRA ticket for that reported in Mongo.