想拉出数组内的特定对象

时间:2018-09-05 08:47:29

标签: mongodb

我有一个这样的数据结构,我想从用户数组中提取带有“ isUserFixed” 0的“ userNames”。

{
   "_id" : ObjectId("5b8e7fda725bec7b317b94f6"),
   "email" : "test@test.com",
   "users" : [ 
       {
           "userName" : "user1",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user2",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user3",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user4",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user5",
           "isUserFixed" : 0
       }
   ]
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

db.getCollection('').aggregate([{"$match": {"Your match criteria key": "Your match criteria value"}},
    {"$group" : {"_id" : {"email": "$email"}}},        
    {
                "$project": {
                    "users": {
                        "$filter": {
                            "input": "$users",
                            "as": "user",
                            "cond": {
                                "$in": ["$$user.isUserFixed", [0]]
                            }
                        }
                    }
                }
            }

        ])