Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB : renaming column name in collection [duplicate]

Tags:

mongodb

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

like image 544
daydreamer Avatar asked Jul 20 '12 20:07

daydreamer


People also ask

How do I change the key name in MongoDB?

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.

What is $Set in MongoDB?

$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.


2 Answers

You can use a $rename modifier.

db.collection.update({}, {$rename: {'XUknown': 'XString'}}, false, true);

You might also refresh your knowledge of update().

like image 181
Sergio Tulentsev Avatar answered Oct 21 '22 06:10

Sergio Tulentsev


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});
like image 6
Rizwan Avatar answered Oct 21 '22 07:10

Rizwan