user4408652
user4408652

Reputation:

Mongo DB find by field name with any value

I am trying to find all documents that have this field, it doesn't matter what exact value it contains; I am only interested in the existence of it.

Here is an example:

{  
   "payload":{  
      "products":{  
         ...
      },
      "discount":{  
         "type":"1%",
         "value":"1",
         "name":"New Year Discount 1%"
      }
   }
}

I need to target any document that has a field with the name discount. How can I write a query to get such documents?

Upvotes: 27

Views: 29381

Answers (2)

sepehr
sepehr

Reputation: 18505

You're probably looking for the $exists operator:

db.collection.find({ "payload.discount": { $exists: true } })

Upvotes: 13

mickl
mickl

Reputation: 49985

There's a $exists operator for that:

db.collectionName.find({"payload.discount": {$exists: true}})

Upvotes: 44

Related Questions