How do I query a property with a dot in it?

mongodb

Solution

The profile entry for the dotted query does appear to be a bug .. key names in MongoDB are not meant to include `.` (or a leading `$`).

As you've noticed, this will cause you issues when trying to query because the dot notation is used to indicate embedded objects.

A limited workaround in MongoDB 2.2.0 appears to be using the Aggregation Framework to match the query object as an embedded document:

db.system.profile.aggregate({ $match: {'query': {"antecedents.title" : "The Fireman"}}})

I've reported this profiling bug in MongoDB's issue tracker as SERVER-7349.

Problem

Generally, MongoDB doesn't let one have dots in property names. For example: ``` db.books.insert({"protagonist.name": "Guy Montog"}); ``` fails with the error: ``` uncaught exception: can't have . in field names [protagonist.name] ``` However, I ran across a situation in which property names do have dots. This can occur in the system.profile collection. Here's an example: ``` db.setProfilingLevel(2); db.books.insert({protagonist: "Guy Montog", antecedents: [{title: "The Fireman"}, {title: "Bright Phoenix"}]}); db.books.find({"antecedents.title": "The Fireman"}) ``` Now If I look at the system.profile collection, I see the following record: ``` > db.system.profile.findOne({op: "query"}) { "ts" : ISODate("2012-10-14T00:05:49.896Z"), "op" : "query", "ns" : "wordswing.books", "query" : { "antecedents.title" : "The Fireman" }, "nscanned" : 1, "nreturned" : 1, "responseLength" : 153, "millis" : 0, "client" : "127.0.0.1", "user" : "" } ``` Suppose I wanted to query the for system.profile documents that queried "antecedents.title"? This seems to be a problem because there is a dot in the property name. I tried all of the following: ``` db.system.profile.find({'query.antecedents.title': 'The Fireman'}) db.system.profile.find({'query.antecedents\.title': 'The Fireman'}) db.system.profile.find({"query.antecedents\.title": 'The Fireman'}) ``` None of which worked. Ideas? This is really interfering with my ability to poke around a rather large system.profile collection. Thanks in advance. Update In response to a comment, the version I'm using is: ``` $ mongod --version db version v2.0.6, pdfile version 4.5 Sun Oct 14 18:43:29 git version: e1c0cbc25863f6356aa4e31375add7bb49fb05bc ``` Kevin

Original source