1

I want to convert an int array to strings. My int:

"key" : 280

If I have only 1 integer to change i can use:

db.collection.find( { 'key' : { $type : 16 } } ).forEach( function (x) { x.key = ""+x.key; db.collection.save(x);});

$type 16 describes an 32-bit integer. With that code all looks fine.

But if I have an array like:

"key" : 280, 193, 213

and I use this code I get:

"key" : "280,193,213"

but I want -> "key" : "280","193","213"

Have anybody an solution ?

1 Answer 1

1

Please try this:

db.collection.find( { 'key.0' : { $type : 16 } } ).forEach( function (x) {
    var arr = [];
    x.key.forEach( function (e) { arr.push("" + e); } );
    x.key = arr;
    db.collection.save(x);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.