Evan Teran
Evan Teran

Reputation: 90422

mongo dot notation ambiguity

I love MongoDB, and a certain little ambiguity occurred to me and I was wondering if anyone had seen this before and possibly would know the answer :-).

in mongo, to reach into sub-objects, you use dot notation, for example:

db.persons.find({ "address.state" : "CA" })

which is simple enough. How (if it does at all) does mongo deal with the difference between:

{
    "address" { "state" : "CA" }
}

and

{
    "address.state" : "CA"
}

since dots are legal in keys as far as i know. Additionally, I believe that this would be a legal doc as well:

{
    "address" { "state" : "A" },
    "address.state" : "B"
}

in which case, I can see this query returning either "A" or "B":

db.persons.find({}, {"address.state"}) // all docs selecting address.state as result.

Similar potential issue can arise I imagine with arrays as well:

{"a":["test"]}

which could be access with:

{"a.0"}

and of course

{"a" {"0" : "test"} }

which would also be access with:

{"a.0"}

thoughts? experiences? Is the conventional wisdom simply not to do that?

Upvotes: 3

Views: 1416

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311845

A key such as "address.state" isn't legal. From here:

Field names cannot contain dots (i.e. .) or null characters, and they must not start with a dollar sign (i.e. $).

Upvotes: 8

Related Questions