Mongodb搜索嵌套数组元素

时间:2017-04-27 18:10:47

标签: mongodb mongodb-query

我有以下数据。想要搜索任何元素(CRT,READ,DLT,UPD)与输入数组匹配的aclpermissions。

查询

db.AMSAppACL.find({"aclpermissions.READ" : {'$in': ['58dc0bea0cd182789fc62fab']}}).pretty();

仅搜索READ元素。有没有办法搜索所有元素而不是使用或查询和聚合

{
        "_id" : ObjectId("5900d6abb9eb284a78f5a350"),
        "_class" : "com.debopam.amsapp.model.AMSAppACL",
        "attrUniqueCode" : "USER",
        "attributeVersion" : 1,
        "aclpermissions" : {
                "CRT" : [
                        "58dc0bd70cd182789fc62faa"
                ],
                "READ" : [
                        "58dc0bd70cd182789fc62faa",
                        "58dc0bea0cd182789fc62fab"
                ],
                "UPD" : [
                        "58dc0bd70cd182789fc62faa"
                ],
                "DLT" : [
                        "58dc0bd70cd182789fc62faa"
                ]
        },
        "orgHierachyIdentifier" : "14",
        "orgid" : 14,
        "createDate" : ISODate("2017-04-26T17:19:39.026Z"),
        "lastModifiedDate" : ISODate("2017-04-26T17:19:39.026Z"),
        "createdBy" : "appadmin",
        "lastModifiedBy" : "appadmin"
}

1 个答案:

答案 0 :(得分:0)

您应该尝试将动态密钥的aclpermissions部分更新为标记密钥。

"aclpermissions":
     [
       {k:"CRT", v: ["58dc0bd70cd182789fc62faa"]},
       {k:"READ", v: [ "58dc0bd70cd182789fc62faa", "58dc0bea0cd182789fc62fab"]}....
     ]

现在,您可以将查询从帖子更新为

db.AMSAppACL.find({"aclpermissions.v" : {'$in': ['58dc0bea0cd182789fc62fab']}}).pretty();