如何查询MongoDB中的嵌套对象?

时间:2016-01-15 22:28:59

标签: json mongodb mongodb-query

我有一个如下所示的JSON模式:

{
    "_id" : ObjectId("5692a3e124de1e0ce2dfda22"),
    "title" : "A Decade of Decadence, Pt. 2: Legacy of Dreams",
    "year" : 2013,
    "rated" : "PG-13",
    "released" : ISODate("2013-09-13T04:00:00Z"),
    "runtime" : 65,
    "countries" : [
            "USA"
    ],
    "genres" : [
            "Documentary"
    ],
    "director" : "Drew Glick",
    "writers" : [
            "Drew Glick"
    ],
    "actors" : [
            "Gordon Auld",
            "Howie Boulware Jr.",
            "Tod Boulware",
            "Chen Drachman"
    ],
    "plot" : "A behind the scenes look at the making of A Tiger in the Dark: The Decadence Saga.",
    "poster" : null,
    "imdb" : {
            "id" : "tt2199902",
            "rating" : 8,
            "votes" : 50
    },
    "awards" : {
            "wins" : 0,
            "nominations" : 0,
            "text" : ""
    },
    "type" : "movie"
}

我正在努力寻找2013年发布的电影,该电影被评为PG-13并且没有获奖。我在Mongo Shell中尝试了以下查询,但没有运气:

db.movieDetails.find({rated: "PG-13", year:2013, awards: { wins : 0}})

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

来自文档:

  

当字段包含嵌入文档时,查询可以指定嵌入文档的完全匹配,也可以使用dot notation

指定嵌入文档中各个字段的匹配项
db.movieDetails.find( { "rated": "PG-13", "year": 2013, "awards.wins" : 0 } )

答案 1 :(得分:0)

如果这个PG-13评级查询适合您:

db.movieDetails.find({"rated": "PG-13"})

然后我会尝试这样的事情:

db.movieDetails.find({$and: [{"rated": "PG-13"}, {"year": 2013}, {"awards": { "wins" : 0}}]} )