Ravi Shah
Ravi Shah

Reputation: 82

MongoDB "The dollar ($) prefixed field is not valid for storage."

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})

Upvotes: 2

Views: 12259

Answers (3)

Charlie
Charlie

Reputation: 2263

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

Upvotes: 3

Tom Slabbaert
Tom Slabbaert

Reputation: 22276

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}
)

Upvotes: 1

enffinity
enffinity

Reputation: 1

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

Upvotes: 0

Related Questions