0

I have a collection that has some fields that should be integers. Due to a software bug, some of these values are strings. I need to convert them. Here is an example document:

var coll = db.getCollection('info');
coll.find({ id: "985517" });

Result:

{
    "_id" : "41cbb4e48284c0612a1c4b3ee5e3e2720c01197a",
    "id" : "985517",
    "title" : "009_1a.jpg",
    "ext" : ".jpg",
    "content_type" : "image/jpeg; charset=utf-8",
    "sha1" : "41cbb4e48284c0612a1c4b3ee5e3e2720c01197a",
    "has_thumb" : true,
    "size" : 2917,  
    "width_px" : "70",
    "height_px" : "69"
}

I try to convert width_px and height_px values into integers. My first naive try:

var coll = db.getCollection('info');
var bulk = coll.initializeOrderedBulkOp();
var counter = 0;
coll.find({ width_px: { $type: "string" }, id: "985516" }).limit(100).forEach(function(data) {
    var updoc = {
        "$set": {}
    };
    updoc["$set"]["width_px"] = parseInt(data.width_px);
    // queue the update
    bulk.find({
        "_id": data._id
    }).update(updoc);
    counter++;
    // Drain and re-initialize every 1000 update statements
    if (counter % 1000 == 0) {
        bulk.execute();
        bulk = coll.initializeOrderedBulkOp();
    }
    })
    // Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();

The result is dissapointing:

{
    "_id" : "41cbb4e48284c0612a1c4b3ee5e3e2720c01197a",
    "id" : "985517",
    "title" : "009_1a.jpg",
    "ext" : ".jpg",
    "content_type" : "image/jpeg; charset=utf-8",
    "sha1" : "41cbb4e48284c0612a1c4b3ee5e3e2720c01197a",
    "has_thumb" : true,
    "size" : 2917,  
    "width_px" : 70.0,
    "height_px" : "69"
}

So it was updated to double, instead of int32. I understand that in JavaScript, there is no separate double and integer type, just "number". But I need to update 100 000 documents and I want to do this efficiently.

But how?

  • ,You can easily update data type field in MongoDB through compass. – Md Haidar Ali Khan Jul 17 '18 at 11:34
  • Okay, but compass is just a GUI. I want to know the exact command that will change the type on the server. I do not want to get help about using user interfaces. I want to acquire real knowledge. :-) – nagylzs Jul 17 '18 at 11:44
1

As per MongoDB documentation here From the Documents tab in MongoDB Compass, you can view, insert, clone, modify, and delete documents .

First you have to connect your mongod with MongoDB Compass then you shall able to change your desire data type of their respective field in MongoDB.

Change Field Type

You can change the data type of a field by using the data type selectors on the right of the field.

enter image description here

Note: Creating, deleting, editing and cloning documents is not permitted in MongoDB Compass Readonly Edition.

For your further ref here

| improve this answer | |
  • I'm upvoting this because he metioned the type documentation. But the question was not about using a GUI or changing the type of a single document... – nagylzs Jul 17 '18 at 12:14
1

The solution is to use NumberInt instead of parseInt.

var coll = db.getCollection('info');
var bulk = coll.initializeOrderedBulkOp();
var counter = 0;
coll.find({ width_px: { $type: "string" }}).forEach(function(data) {
    var updoc = {
        "$set": {}
    };
    updoc["$set"]["width_px"] = NumberInt(data.width_px);
    // queue the update
    bulk.find({
        "_id": data._id
    }).update(updoc);
    counter++;
    // Drain and re-initialize every 1000 update statements
    if (counter % 1000 == 0) {
        bulk.execute();
        bulk = coll.initializeOrderedBulkOp();
    }
    })
    // Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();

It will construct a new NumberInt object that is mongodb specific, and it will be serialized as an int32.

| improve this answer | |
0

change str to int

db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) { 
obj.field-name = new NumberInt(obj.field-name);
db.db-name.save(obj); });
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.