MongoDB: Query over a hash with special chars in keys MongoDB: Query over a hash with special chars in keys database database

MongoDB: Query over a hash with special chars in keys


according to the source below, you can use any UTF8 character in the field name -- the only exception is the '.' character which is not allowed in field names (because it's used to query sub-documents), and field names can't start with a '$' character..

See:

https://jira.mongodb.org/browse/SERVER-3229


If you know the key name you can just query by

db.foo.find({'account_profile.discount_pct' : '15'})

check out the test data

> db.foofoo.insert({name:'ram',account_profile : {"Client code": "0123456789",'discount_pct' : 2}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "0123456789",'discount_pct' : 2}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_pct' : 5}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_pct' : 2}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount %' : 2}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_pct' : 4}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_%' : 4}})> db.foofoo.insert({name:'ram',account_profile : {"Client code": "01236789",'discount_%' : 2}})> db.foofoo.find({'account_profile.discount_%': 2}){ "_id" : ObjectId("4eb0c9965325a7760cfda3db"), "name" : "ram", "account_profile" : { "Client code" : "01236789", "discount_%" : 2 } }> db.foofoo.find({'account_profile.discount_pct': 2}){ "_id" : ObjectId("4eb0c9725325a7760cfda3d5"), "name" : "ram", "account_profile" : { "Client code" : "0123456789", "discount_pct" : 2 } }{ "_id" : ObjectId("4eb0c97c5325a7760cfda3d7"), "name" : "ram", "account_profile" : { "Client code" : "01236789", "discount_pct" : 2 } }

Thanks to @Tilo for pointing out in the above comment, you can't have the period character '.' in the field name, since its representing the dot notation.