3

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?

4

1 回答 1

8

诸如“address.state”之类的键是不合法的。从这里

字段名称不能包含点 (ie .) 或空字符,并且它们不能以美元符号 (ie $) 开头。

于 2012-04-26T12:49:53.410 回答