Ionică Bizău
Ionică Bizău

Reputation: 113335

Mongo subfield dot notation containing special characters

If I have the following object:

{
  someMapping: {
     "some key with spaces & special characters": true,
     "simple_key": true
  }
}

How can I find this document using the dot notation query? In other words, how can I use $exists (which doesn't seem to play well without dot notation) to find it?

I tried:

> db.mycol.findOne({ someMapping: { "some key with spaces & special characters": { $exists: true } } })
null
> db.mycol.findOne({ someMapping: { simple_key: { $exists: true } } })
null
> db.mycol.findOne({ "someMapping['some key with spaces & special characters']": { $exists: true } })
null
> db.mycol.findOne({ "someMapping.simple_key": { $exists: true } })
true

Upvotes: 0

Views: 319

Answers (1)

sidgate
sidgate

Reputation: 15234

Following should work

db.mycol.findOne({ "someMapping.some key with spaces & special characters": 
    { $exists: true } })

More information at mongo docs

db.mycol.findOne({ "someMapping.some key with spaces & special characters": true })

Edit: Mongodb does not support dot and dollar sign in field name

Note: $exists returns true if field exists. If you want to compare field value to true just run

Upvotes: 1

Related Questions