Simple Question
I have MongoDb and I want to change the name of the _id
property to make it MatchId
but this does not work.
It is worth mentioning that all other properties (Except the _id
) could be renamed,
I have no problem with that
Can the _id
property be renamed in a BsonElement?
Or it must have _id
name and can not be changed?
I searched the documentation but found nothing
BsonClassMap.RegisterClassMap<MatchState>(cm =>
{
// this does NOT rename the _id property to "MatchId"
cm.MapIdProperty(x => x.MatchId).SetElementName("MatchId");
// this DOES rename the "MatchCreator" property to "Creator" property
cm.MapProperty(x => x.MatchCreator).SetElementName("Creator");
});
CodePudding user response:
Maybe copy _id
into MatchId
is what you need.
db.collection.update({},
[
{
"$set": {
"MatchId": "$_id"
}
}
],
{
"multi": true
})
CodePudding user response:
Unfortunately, _id is an immutable field in MongoDB and it does not allow to change the _id of a document after you have inserted it. Once set, you cannot update the value of the _id field nor can you replace an existing document with a replacement document that has a different _id field value.