Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

mongodb - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. $).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...