2

While storing the key which have Dollar($) prefixed key is throwing error as "The dollar ($) prefixed field is not valid for storage." in MongoDB.

I am using MongoDB version 4.2 and NodeJS MongonDB Driver 3.5.9.

Example Snippet

db.collection.updateOne({_id: 'ObjectId("618bb1ccd7b16e4232dcb4e8")'}, {$set: {'$name': 'Alex'}}, {upsert: true, checkKeys: false})

3 Answers 3

4

mongodb 4.0 doesn't support top level field with dollar sign. You can upgrade to mongodb 5.0

https://docs.mongodb.com/v4.0/core/document/#field-names

Sign up to request clarification or add additional context in comments.

Comments

1

You just flat out have a syntax error, from the update docs, the update document

Contains only update operator expressions.

In your case you want to be using $set, like so:

db.collection.updateOne(
  {_id: 'ObjectId("618bb1ccd7b16e4232dcb4e8")'},
  { $set: { name: 'Alex'} },
  {upsert: true, checkKeys: false}
)

3 Comments

I am already using $set, it was just I didn't mentioned in the description. I have updated the description.
So instead of "$name" write "name"?
Those keys comes on the fly. I don't have control on that.
0

maybe check in the mongodb file explorer if the object has a $ anywhere?

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Didn't get that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.