2016-11-11 36 views
1

我想插入一個包含以'$'(例如:'$count')開頭的密鑰的json對象的集合。我讀了MongoDB的V3.0 FAQ's,他們已經提到它不是這樣的關鍵。有沒有什麼迂迴的方式插入這樣一把鑰匙並將其取回?

回答

4

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. 「.」).

是不是recomanded,但你可以試試這個:

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"), "." : "$" } 
+0

能否請您闡述關於我們如何使用的片段? – AbiSivam

+1

基本上,你會經歷所有你的價值轉換$或。它是代碼片段所示的unicode表示形式 – Sammaye