mongo点表示法不明确 [英] mongo dot notation ambiguity
问题描述
我爱MongoDB,我有点模棱两可,我想知道是否有人以前见过这个,并且可能会知道答案:-).
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 :-).
在mongo中,要进入子对象,请使用点表示法,例如:
in mongo, to reach into sub-objects, you use dot notation, for example:
db.persons.find({ "address.state" : "CA" })
这很简单. mongo如何处理(如果有的话)以下两者之间的区别:
which is simple enough. How (if it does at all) does mongo deal with the difference between:
{
"address" { "state" : "CA" }
}
和
{
"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"
}
在这种情况下,我可以看到此查询返回"A"
或"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"}
当然
{"a" {"0" : "test"} }
也可以通过以下方式访问:
which would also be access with:
{"a.0"}
有什么想法?经验?传统的智慧仅仅是不这样做吗?
thoughts? experiences? Is the conventional wisdom simply not to do that?
推荐答案
诸如"address.state"之类的密钥不合法.来自此处:
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.$
).
这篇关于mongo点表示法不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!