None of the following filters worked when I tried to update a document, in JS/Node.js filtered by "_id", where the value is the generic ObjectId (ObjectId('6365050650c6be6d5d090135')
) :
{_id : ObjectId(123....7890)}
{_id : ObjectId('123....7890')}
{_id : "ObjectId('123....7890')"}
+ other variantions I came across
Nothing worked for me. I got ReferenceError: ObjectId is not defined
or no errors.
2
Answers
You need to be able to spawn new ObjectIds, so enable that first:
and filter like this:
Actually this is what MongoDB Compass "exports to language" (see screenshot above) when you filter
I am pretty sure this is obvious to many, but it took me quite a while to resolve it, so I hope this saves you some time.
Either use ObjectId from mongodb client like so:
or if you are using mongoose then:
or with mongoose you can directly pass in the objectid string and the mongoose model will handle it for you like so:
I am not sure if the above possible can be done with mongodb client too.