0

Here I have two Documents, I need to find if the array exists with "admin", Which is the best way $in or $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")                    
}

Consider the two document and i need to find the document where "admin" exists

janniks
  • 2,942
  • 4
  • 23
  • 36
ABDUL JAMAL
  • 452
  • 7
  • 12
  • 3
    You cannot find documents using `$in` here... `$exists` is only the way... `db.collection.find({ "roles.admin": { "$exists": true } })` – Ashh Aug 21 '18 at 07:32

1 Answers1

0

Use the mongoDB $exists query operator:

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

Syntax:

{ field: { $exists: <boolean> } }
janniks
  • 2,942
  • 4
  • 23
  • 36