如果您知道键名,您可以通过查询
db.foo.find({'account_profile.discount_pct' : '15'})
查看测试数据
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "0123456789",'discount_pct' : 2}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "0123456789",'discount_pct' : 2}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_pct' : 5}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_pct' : 2}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount %' : 2}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_pct' : 4}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_%' : 4}})
> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_%' : 2}})
> db.foofoo.find({'account_profile.discount_%': 2})
{ "_id" : ObjectId("4eb0c9965325a7760cfda3db"), "name" : "ram", "account_profile" : { "Client code" : "01236789", "discount_%" : 2 } }
> db.foofoo.find({'account_profile.discount_pct': 2})
{ "_id" : ObjectId("4eb0c9725325a7760cfda3d5"), "name" : "ram", "account_profile" : { "Client code" : "0123456789", "discount_pct" : 2 } }
{ "_id" : ObjectId("4eb0c97c5325a7760cfda3d7"), "name" : "ram", "account_profile" : { "Client code" : "01236789", "discount_pct" : 2 } }
感谢@Tilo 在上述comment 中指出,您不能使用句点字符'.'在字段名称中,因为它表示点符号。