如何在mongodb中查询子文档?

时间:2015-02-04 14:41:36

标签: 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”})

此致 斯里卡特

1 个答案:

答案 0 :(得分:0)

Actualy有两种方法可以做到:

  1. db.property.find ( { "_id": "c2", "commercial.address.city": "c2" }, {"commercial.address.city": true} )
  2. db.property.find ( { "_id": "c2", "commercial": { "address" : { "city": "c2" }}} )
  3. 两种情况的区别在于,在第一种情况下,如果您有索引index,则mongo将跟随commercial.address.city,而第二种情况则不会,因此我会使用第一种