Retrive嵌套数组文档

时间:2017-03-01 07:37:46

标签: mongodb mongodb-query aggregation-framework

    {

        "id" : "Sir3GHMQ",
        "name" : "Medavakkam",
        "userList" : [ 
            {
                "loginName" : "venkat@gmail.com",
                "role" : "ADMIN"
            }, 
            {
                "loginName" : "sam@gmail.com",
                "role" : "Operator"
            }
        ]
    }


    {

        "id" : "Sir3GHER",
        "name" : "Medavakkam",
        "userList" : [ 
            {
                "loginName" : "venkat@gmail.com",
                "role" : "OPERATOR"
            },
{
                "loginName" : sam@gmail.com",
                "role" : "OPERATOR"
            }
        ]
    }

在集合中,我需要检索文档,其中userList. loginame="venkat@gmail.com"也检查角色是“admin”的位置。其中role是admin意味着检索所有userList:LoginName及其角色,否则只检索userList:loginName及其角色,无论它是什么。

我试过了:

db.Site.aggregate([ 
    { "$match": { "userList.loginName": "venkat@gmail.com" } }, 
    { "$redact": { 
        "$cond": [ 
            { "$eq": [ 
                { "$ifNull" [ "$loginName", "venkat@gmail.com" ] }, 
                "venkat@gmail.com" 
            ] }, 
            "$$DESCEND", 
            "$$PRUNE" 
        ] 
    } } 
])

我需要这样的输出

{

    "id" : "Sir3GHMQ",
    "name" : "Medavakkam",
    "userList" : [ 
        {
            "loginName" : "venkat@gmail.com",
            "role" : "ADMIN"
        }, 
        {
            "loginName" : "sam@gmail.com",
            "role" : "Operator"
        }
    ]
}


{

    "id" : "Sir3GHER",
    "name" : "Medavakkam",
    "userList" : [ 
        {
            "loginName" : "venkat@gmail.com",
            "role" : "OPERATOR"
        }

    ]
}

1 个答案:

答案 0 :(得分:3)

使用此命令,

          db.f.aggregate([{
            $match: {
                "userList.loginName": "venkat@gmail.com"
            }
        }, {
            "$redact": {
                "$cond": [{
                    $or: [{
                        "$eq": [{
                                "$ifNull": ["$loginName", "venkat@gmail.com"]
                            },
                            "venkat@gmail.com"
                        ]
                    }, {
                        "$eq": [{
                            $setIsSubset: [{
                                $literal: [{
                                    loginName: "venkat@gmail.com",
                                    role: "ADMIN"
                                }]
                            }, "$$ROOT.userList"]
                        }, true]
                    }]
                }, "$$DESCEND", "$$PRUNE"]
            }
        }]).pretty()

OutputData:

      {
    "_id" : ObjectId("58b697e406169b8451ba4cd2"),
    "id" : "Sir3GHMQ",
    "name" : "Medavakkam",
    "userList" : [
            {
                    "loginName" : "venkat@gmail.com",
                    "role" : "ADMIN"
            },
            {
                    "loginName" : "sam@gmail.com",
                    "role" : "Operator"
            }
    ]
   }

  {
    "_id" : ObjectId("58b74e91c568ace843ee17c1"),
    "id" : "Sir3GHER",
    "name" : "Medavakkam",
    "userList" : [
            {
                    "loginName" : "venkat@gmail.com",
                    "role" : "OPERATOR"
            }
    ]
 }  

希望这会对你有所帮助。

Java代码:

              MongoClient mongoClient = new MongoClient();
              MongoDatabase database = mongoClient.getDatabase("test");
              MongoCollection<Document> collection = database.getCollection("f");

                List<Document> results = collection.aggregate(Arrays.asList(new Document("$match",new Document().append("userList.loginName", "venkat@gmail.com")),
                        new Document("$redact", new Document("$cond",
                                Arrays.asList(new Document("$or",Arrays.asList(new Document("$eq",
                                        Arrays.asList(new Document("$ifNull", Arrays.asList("$loginName", "venkat@gmail.com")), "venkat@gmail.com")),new Document("$eq", Arrays.asList(new Document("$setIsSubset", Arrays.asList(new Document("$literal", Arrays.asList(new Document().append("loginName", "venkat@gmail.com").append("role", "ADMIN"))),"$$ROOT.userList")), true)))), 
                                "$$DESCEND", "$$PRUNE")))

            )).into(new ArrayList<Document>());

                for(Document docs: results){
                    System.out.println(docs.toJson());
                }