There is a question already in Stackoverflow, very similar with my question. The thing is that the answer for that questions was for a Java Driver, I am trying to do it in the shell.

I am doing this...

db.meta.update({'fields.properties.default': { $type : 1 }}, {'fields.properties.default': { $type : 2 }})

This is not working!


Answers

The only way to change the $type of the data is to perform an update on the data where the data has the correct type.

In this case, it looks like you're trying to change the $type from 1 (double) to 2 (string).http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24type

So simply load the document from the DB, perform the cast (new String(x)) and then save the document again.

If you need to do this programmatically and entirely from the shell, you can use thefind(...).forEach(function(x) {}) syntax.


In response to the second comment below. Change the field bad from a number to a string in collectionfoo.

db.foo.find( { 'bad' : { $type : 1 } } ).forEach( function (x) {   
  x.bad = new String(x.bad); // convert field to string
  db.foo.save(x);
});
share|improve this answer
Thanks! This is what I was looking for! – J. Quintas Feb 12 '11 at 19:12
Any chance of an example - changing a field type from int to string (or vice versa), from the shell? – Alister Bulman Mar 16 '11 at 13:30
Thanks @gates, you are a star. If I could, I'd up-vote you twice. :-) – Alister Bulman Mar 23 '11 at 10:14
4 
in case Int32->String, new String(x.bad) creates collection of Strings with 0-index-item x.badvalue. Variant ""+x.bad, described by Simone works as desired - creates String value instead of Int32 – Dao Jul 30 '12 at 16:36

// String 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); } );

// Integer to String

db.db-name.find({field-name : {$exists : true}}).forEach( function(obj) { obj.field-name = ""+obj.field-name; db.db-name.save(obj); } );
share|improve this answer
This is great - do you know how you'd convert a string (think currency like '1.23') to the integer 123? I assume you'd have to parse it as a float or decimal, multiply it by 100, then save it as an integer, but I can't find the right docs to do this. Thanks! – Brian Armstrong Feb 23 '12 at 5:59
Actually this working is good. But I have an application running with mongoid 2.4.0-stable which has fields such as field: customer_count, type: Integer & a validation as validates_numericality_of :customer_count which was working fine. Now when I am upgrading to mongoid to 3.0.16, when I assign a string value it automatically converts it to 0. without an error. I want to throw an error on wrong data assignment, this behavior turning out strange for me. – Swapnil Chincholkar Feb 1 at 11:37
1 
I ran this and got the error: Error: could not convert string to integer (shell):1 – Mittenchops Mar 21 at 21:04

This is what I used for string to int conversion.

db.my_collection.find().forEach( function(obj) {
    obj.my_value= parseInt(obj.my_value);
    db.my_collection.save(obj);

});



출처 - http://stackoverflow.com/questions/4973095/mongodb-how-to-change-the-type-of-a-field







'DB > MongoDB' 카테고리의 다른 글

mongodb - backup  (0) 2014.02.23
mongo - TTL  (0) 2014.01.15
mongodb - Query, Update and Projection Operators  (0) 2013.07.23
mongodb - GridFS  (0) 2013.06.18
mongodb - binary data(type) 저장  (0) 2013.06.18
Posted by linuxism
,