如何使用嵌入式文档查询mongodb

时间:2016-09-07 10:25:01

标签: mongodb

嗨,我正在练习mongodb而且我遇到了问题。我是以下几套文件。

{
    "_id" : ObjectId("57cf9a134607674792dbad9e"),
    "address" : {
        "building" : "351",
        "coord" : [ 
            -73.9851356, 
            40.7676919
        ],
        "street" : "West   57 Street",
        "zipcode" : "10019"
    },
    "borough" : "Manhattan",
    "cuisine" : "Irish",
    "grades" : [ 
        {
            "date" : ISODate("2014-09-06T00:00:00.000Z"),
            "grade" : "A",
            "score" : 2
        }, 
        {
            "date" : ISODate("2013-07-22T00:00:00.000Z"),
            "grade" : "A",
            "score" : 11
        }, 
        {
            "date" : ISODate("2012-07-31T00:00:00.000Z"),
            "grade" : "A",
            "score" : 12
        }, 
        {
            "date" : ISODate("2011-12-29T00:00:00.000Z"),
            "grade" : "A",
            "score" : 12
        }
    ],
    "name" : "Dj Reynolds Pub And Restaurant",
    "restaurant_id" : "30191841"
}

我想获取zipcode为10019的所有文档的列表 我正在关注mongodb数据库教程,我已经尝试了以下查询,但似乎没有任何工作,我得到零错误。

 db.restaurants.find({address:{zipcode:10019}});
    db.restaurants.find({"address.zipcode":10019})

1 个答案:

答案 0 :(得分:3)

zipcode是一个字符串,因此您的查询应为

db.restaurants.find({ "address.zipcode": "10019" })

而不是

db.restaurants.find({ "address.zipcode": 10019 })