Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo: query by key one level deep

Tags:

Is it possible to use something like 'exists' in a Mongo query to return this record based on an ID?

Something like select where 'ids' contains key '123456'?

  {
      "department": "Digging",
      "ids": {
        "123456": {
          "color": "blue"
        },
        "123457": {
          "color": "red"
        }
      }
    }
like image 927
axiomx11 Avatar asked Mar 19 '12 18:03

axiomx11


1 Answers

As you're searching for the existence of a field with a given name, $exists is the operator you need (see Advanced Queries).

e.g. something like:

db.YourCollection.find({ "ids.123456" : {$exists: true}});
like image 100
AdaTheDev Avatar answered Oct 01 '22 12:10

AdaTheDev