gpt4 book ai didi

MongoDB 插入 key 为 '$'(美元)

转载 作者:行者123 更新时间:2023-12-02 09:39:07 26 4
gpt4 key购买 nike

我正在尝试插入一个包含 json 对象的集合,该对象包含以“$”开头的键(例如:“$count”)。我读了mongodb v3.0 FAQ's他们提到这不是一把 key 。有没有什么迂回的方法插入这样的 key 并将其取回?

最佳答案

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

<小时/>

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. “.”).

不推荐,但你可以尝试这个:

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 插入 key 为 '$'(美元),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com