Answer the question
In order to leave comments, you need to log in
How to search for a document by a field inside a field (nested element)?
Good afternoon. I decided to go to the Mongo virtual console, but immediately the problem arose of searching for a document by an array attribute.
Tell me how it is correct, and whether it is possible to implement such a request in principle
> db.coltest.save({ a:1,b:"hello",c:{c1:"1", c2:"hello"}});
> db.coltest.save({ a:2,b:"goodbye",c:{c1:"2", c2:"gotohell"}});
> db.coltest.find();
[
{ "a" : 1, "b" : "hello", "c" : { "c1" : "1", "c2" : "hello" }, "_id" : { "$oid" : "506990cccc93742e0d0e5ac2" } },
{ "a" : 2, "b" : "goodbye", "c" : { "c1" : "2", "c2" : "gotohell" }, "_id" : { "$oid" : "5069911dcc93742e0d0e5ac8" } }
]
> db.coltest.find({a:1});
[
{ "a" : 1, "b" : "hello", "c" : { "c1" : "1", "c2" : "hello" }, "_id" : { "$oid" : "506990cccc93742e0d0e5ac2" } }
]
> db.coltest.find({b:"goodbye"});
[
{ "a" : 2, "b" : "goodbye", "c" : { "c1" : "2", "c2" : "gotohell" }, "_id" : { "$oid" : "5069911dcc93742e0d0e5ac8" } }
]
>db.coltest.find({c:{c1:"1"}});
[
]
>db.coltest.find({c:{c2:"gotohell"}});
[
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question