[英]how to query sub document in mongodb?
如何在子文檔中查詢子文檔?
如何在商業文件中檢索城市名稱。
{
"_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”})
問候Sreekanth
實際上,有兩種方法可以做到這一點:
db.property.find ( { "_id": "c2", "commercial.address.city": "c2" }, {"commercial.address.city": true} )
db.property.find ( { "_id": "c2", "commercial": { "address" : { "city": "c2" }}} )
兩種情況的區別在於,在第一種情況下,如果您有一個索引commercial.address.city
,mongo將遵循該index
,第二種則不會,因此我建議您使用第一個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.