F
F
FilimoniC2012-10-01 16:05:18
MongoDB
FilimoniC, 2012-10-01 16:05:18

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

1 answer(s)
Y
yurtaev, 2012-10-01
@FilimoniC

db.coltest.find({"c.c1": "1"});– docs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question