Sunday, 15 August 2010

mongodb : how to get documents that contains nested field -



mongodb : how to get documents that contains nested field -

how can documents contains nested field f5=12?

db.products.insert( { f: "card", f1: {f2 : {f3 : 15 } } } )

db.products.insert( { f: "card", f1: {f2 : {f4 : {f5 : 12} } } } )

db.products.insert( { f: "card", f1: {f2 : {f3 : {f5 : 43} } } } )

db.products.insert( { f: "card", f1: {f2 : {f4 : 98 } } } )

i've tried smth this:

db.products.find($where : "this.content.indexof('f5 : 12') != -1")

db.products.find( {$elemmatch : {f5 : 12} })

but doesn't work.. has ideas?

you can utilize dot notation in query this:

class="lang-js prettyprint-override">db.products.find({'f1.f2.f4.f5': 12})

if want more flexible don't have specify each of parent keys, you'd need search docs using $where operator.

mongodb

No comments:

Post a Comment