Cannot update field of type ObjectId in MongoDB


#1

I’m trying to use the REST API to update a field in a record in MongoDB. The field is of type ObjectId, but the API refuses to accept the Extended JSON syntax for an ObjectId.

I’m using a PATCH request with the following body:

{
  "testVal": { "$oid": "5a31edc2743bb8f105db877d" }
}

The response I get is:

{
  "error": {
    "code": 500,
    "message": "The dollar ($) prefixed field '$oid' in 'testVal.$oid' is not valid for storage."
  }
}

I have tested the Extended JSON syntax for a Date (i.e. the “$date” syntax) and that works fine, so it seems to be a problem with ObjectIds specifically. I have also tried the mongo shell syntax of ObjectId(“5a31edc2743bb8f105db877d”), but that doesn’t work either.

Can anyone tell me how to update an ObjectId field?


#2

@foobarius Our interface uses the $id label (gets converted to ObjectId() internally, seemingly the “old” way, but still works). $oid usage seems to be an evolution with the transition to the new drivers. We will add the $oid support, but until that is released please see if this scenario works with $id for you.