I have a schema that looks like
name:
value:
pattern:
XUknown:
I have 2 million documents in this collection.
Want
- I want to rename the column name XUknown
to XString
, so that the schema looks like
name:
value:
pattern:
XString:
How can I achieve this?
Thank you
MongoDB provides different types of field update operators to update the values of the fields of the documents and $rename operator is one of them. This operator is used to update the names of the fields with new names. The new name of the field should be different from the existing name of the field.
$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields . Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.
You can use a $rename modifier.
db.collection.update({}, {$rename: {'XUknown': 'XString'}}, false, true);
You might also refresh your knowledge of update().
You can rename all the document by specify "Multi true" applicable to all the documents in the collection.
db.collection.update({}, {$rename: {'XUknown': 'XString'}}, {multi:true});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With