mongo dot 符号歧义 [英] mongo dot notation ambiguity

查看:15
本文介绍了mongo dot 符号歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 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 dot 符号歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆