0 votes
1 view
in SQL by (30.7k points)

I am trying to change the type of a field from within the mongo shell.

I am doing this...

db.meta.update( 

{'fields.properties.default': { $type : 1 }}, {'fields.properties.default': { $type : 2 }} 

)

But it's not working!

1 Answer

0 votes
by (73.3k points)

If you want to change the type of a field then convert String field to Integer:-

db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) { 

obj.field-name = new NumberInt(obj.field-name); db.db-name.save(obj); 

});

After that convert Integer field to String:

db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) { 

obj.field-name = "" + obj.field-name; 

db.db-name.save(obj); 

});

...