MongoServerError: FieldPath field names may not start with '$'

Do you mean findOneAndUpdate() method?

Since it looks like OP is trying to update the document, I’m guessing OP wants either updateOne method, or as you suggest the findOneAndUpdate(). The error is definitely caused by passing {$push...etc where find would normally expect a projection.

1 Like

i want to basically push data to that array but it shows this: MongoServerError: FieldPath field names may not start with ‘$’.

i don’t want to update whol, just want to push that data too to that document.

Hi,

But do you want to push that such that the array in the database is updated?

yes basically this would be a chat app and i want that it should get update means the earlier messages should remains the same and a new object should also come there bu im not getting a proper code for that is is showing the error.
[MongoServerError: FieldPath field names may not start with ‘$’]

Using updateOne or findOneAndUpdate, you won’t modify the whole document but only the fields under $push.

The reason why you get the error is that findOne has a blueprint similar to this:

findOne(query, project, options)

And the project document does not expect a field starting with $.

The methods linked before won’t normally error unless those fields in the origin doc aren’t arrays.

So basically you can try using findOneAndUpdate() and tell us of that’s not what you expect, and what is what you expext.

You are using findOne method which finds and returns a document. That’s not how you update documents. That’s all we are saying.

Asya

1 Like

oooh i see sorry for helping me guys! the error is solved now, thankyou!

The conversation has ended.
Thankyou!
dX

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.