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?
Asked
Active
Viewed 7,827 times
6
-
I've provided a solution here: https://stackoverflow.com/questions/12397118/mongodb-dot-in-key-name/52651208#52651208 – Nico Oct 04 '18 at 16:15
1 Answers
8
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"), "." : "$" }

sergiuz
- 5,353
- 1
- 35
- 51
-
-
1Essentially you go through all your values converting $ or . to it's unicode representation as shown in the snippet – Sammaye Nov 11 '16 at 10:10
-
1That is not the "true" dollar character. The usual dollar character is "\u0024", while the character "\uFF04" is a character "similar" to the "true" dollar character. Hence, if you want to store some working update queries in a collection, such as { $set: { name: 'Gianni' } }, you can not, because with that character they would not work. Also try to do ('\uFF04" === '$') in a js console and it will return false. – EuberDeveloper Dec 07 '20 at 21:14