如何基于mongo db中的embeded收集字段进行过滤

时间:2016-05-20 11:19:29

标签: mongodb filter

我有一个类似下面的集合

{ 
    "_id" : ObjectId("573eb77bf3465096ddb1e873"), 
    "id" : 1, 
    "name" : "siva", 
    "email" : "sivateja@gmail.com", 
    "address" : [ 
        { "street" : "balajinager", "district" : "nellore", "state" : "A.p" }, 
        [ { "street" : "balajinager", "district" : "nellore", "state" : "A.p" } ] 
    ] 
}

我想根据city数组内的address来过滤记录,我们怎么能在mongo db中做到这一点?

1 个答案:

答案 0 :(得分:0)

地区还好吗?

{address:{$elemMatch:{district:"nellore"}}}

请参阅https://docs.mongodb.com/manual/reference/operator/query/elemMatch/