I need to send the following example MongoDB query from a React.JS frontend app to a backend Node.js API via URL request params:
{
_id: ObjectId('507f1f77bcf86cd799439011'),
name: {
$in: ['foo', 'bar']
}
}
However, ObjectId is a function and cannot be serialized to JSON. So, I tried this BSON form as proposed in https://stackoverflow.com/a/34486720/6039697:
{
"_id": {
"$oid": "507f1f77bcf86cd799439011"
},
"name": {
"$in": ["foo", "bar"]
}
}
But MongoDB show this error
{
"message" : "unknown operator: $oid",
"ok" : 0,
"code" : NumberInt(2),
"codeName" : "BadValue",
"name" : "MongoError"
}
I Know I could check for _ids and parse in the API, but I would like to make this transparent and automatic. Does anyone have an idea on how can I get this working?
GETon something likemyapi.com/whatever/507f1f77bcf86cd799439011. On you backend, you can simply get that id from the parameters object and pass it to mongoose.