如何查询子文档中的子文档。?
如何在商业文件中检索城市名称。
{
"_id" : " c2 ",
"commercial" : {
" type " : " restaurant ",
" sale_type " : " sale ",
" owner_name " : " josi schmit",
" address " : {
" street " : " kleine rittergasse ",
" plot_no " : 4,
" city " : " frankfurt ",
" state " : " hessen ",
" country " : "germany ",
"postal_code " : 60329,
"email" : " josi123@gmail.com"
},
" total_area " : " 300 sq meters ",
" sale_price(EUR) " : 100000,
" features " : {
" lifts " : 1,
" heating " : true,
" parking " : true,
" kitchen " : true,
" security_cameras " : true,
" furniture " : true
}
}
}
这是我的查询,但我知道我错了请帮助我。
db.property.find({“_ id”:“c2”,“address.city”:“c2”})
此致 斯里卡特
答案 0 :(得分:0)
Actualy有两种方法可以做到:
db.property.find ( { "_id": "c2", "commercial.address.city": "c2" }, {"commercial.address.city": true} )
db.property.find ( { "_id": "c2", "commercial": { "address" : { "city": "c2" }}} )
两种情况的区别在于,在第一种情况下,如果您有索引index
,则mongo将跟随commercial.address.city
,而第二种情况则不会,因此我会使用第一种