I am trying to update ObjectId using mongodb extended json.
I am using the below to insert the document.
collection.insertMany([{ parentQueryGroupId: { $oid :
"628fadb4d370987ac789c0cd" } }])
So it is storing as is like parentQueryGroupId: { $oid : "628fadb4d370987ac789c0cd"
.
But as I need it to store as ObjectId("628fadb4d370987ac789c0cd")
Is this possible directly through extended JSON? Thing is, the server-side query is generic. So I am sending data from the client-side and then directly passing that id to update. But I need it to be ObjectId instead of an object.
I can loop over in such cases where if there is $oid
key is present then I can convert it to ObjectId
, but is this natively supported by Mongoose or MongoDB?
2
Answers
I used
EJSON
frombson
package to convert it to the appropriate format as below.And it is not having issues if it is not extended json. works well without extended types as well (unless you actually want to store those
$oid
format as is because deserialize will convert them too).you can try this,