Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...