MongoDB插入带有$的键(美元) [英] MongoDB Insert key with '$' (dollar)

查看:171
本文介绍了MongoDB插入带有$的键(美元)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入一个带有json对象的集合,该对象包含一个以'$'开头的键(例如:'$count').我阅读了mongodb v3.0 常见问题解答他们提到这不是关键.是否有任何回旋方式插入这样的密钥并将其取回?

I am trying to insert a collection with a json object containing a key which starts with '$' (for instance: '$count'). I read the mongodb v3.0 FAQ's and they have mentioned it is not such a key. Is there any roundabout way insert such a key and retrieve it back?

推荐答案

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

Field names cannot contain dots (i.e. .) or null characters, and they must not start with a dollar sign (i.e. $).


在某些情况下,您可能希望用 用户提供的密钥.在这种情况下,密钥将需要替换 预留的$和.人物.任何字符都足够,但是 考虑使用Unicode全角等效项:U + FF04(即"$") 和U + FF0E(即.").

In some cases, you may wish to build a BSON object with a user-provided key. In these situations, keys will need to substitute the reserved $ and . characters. Any character is sufficient, but consider using the Unicode full width equivalents: U+FF04 (i.e. "$") and U+FF0E (i.e. ".").

不建议这样做,但是您可以尝试以下方法:

Is not recomanded but you can try this:

dollar = "\uFF04";
$
dot = "\uFF0E"
.

db.test.save({[dollar]:dot})
WriteResult({ "nInserted" : 1 })
db.test.save({[dot]:dollar})
WriteResult({ "nInserted" : 1 })

db.test.find()
{ "_id" : ObjectId("58256b0f9934a5d1c696c456"), "$" : "." }
{ "_id" : ObjectId("58256d359934a5d1c696c457"), "." : "$" }

这篇关于MongoDB插入带有$的键(美元)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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