代码之家  ›  专栏  ›  技术社区  ›  ABDUL JAMAL

查找在数组中包含特定值或存在于数组[duplicate]中的文档

  •  0
  • ABDUL JAMAL  · 技术社区  · 7 年前

    这里我有两个文档,我需要找到数组是否存在 "admin" ,哪种方法最好 $in $exists ?

    {
        "url_name" : "List All Active clients",
        "org_url" : "localhost:4523/create_project/active_clients_list",
        "site_url" : "create_project/active_clients_list",
        "roles" : {
            "writer" : [ 
                "get", 
                "post", 
                "put"
            ],
            "admin" : [ 
                "get", 
                "post", 
                "put"
            ]
        },
        "updated_on" : ISODate("2018-08-21T05:09:25.896Z"),
        "created_on" : ISODate("2018-08-21T05:09:25.896Z")
    }, {
        "url_name" : "List All Active clients",
        "org_url" : "localhost:4523/create_project/active_clients_list",
        "site_url" : "create_project/active_clients_list",
        "roles" : {
            "writer" : [ 
                "get", 
                "post", 
                "put"
            ]
        },
        "updated_on" : ISODate("2018-08-21T05:09:34.407Z"),
        "created_on" : ISODate("2018-08-21T05:09:34.407Z")                    
    }
    

    考虑两个文档,我需要找到文档在哪里 “管理员” 存在

    1 回复  |  直到 7 年前
        1
  •  0
  •   janniks    7 年前

    使用mongoDB $exists 查询运算符:

    db.collection.find({ "roles.admin": { "$exists": true } })
    

    语法:

    { field: { $exists: <boolean> } }