is there a way to change a MongoDB collection reference?
With change I mean the value in the reference.
I can’t just create a direct link because the database is in use.
My structure looks like this
{
"_id" : ObjectId("4e7c6b47e4b0dea06288ad21"),
"license" : "ABC123",
"model" : "911",
"make" : "Porsche",
"owner" : {
"$ref" : "users",
"$id" : "Test User"
}
}
I want to change the ID like
{ "$ref" : "users", "$id" : "NEW USER"}
Sure you can, but you can’t do it with a direct $set :
As you can see this breaks the references because the DBRef specification demands the order of the $ref and $id fields to be $ref first.
You can update it like this :
Or just use “new DBRef(..)” if you’re in shell.