Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k 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 (106k 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); 

});

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 4, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...