A
A
alex stephen2016-03-18 14:00:08
MongoDB
alex stephen, 2016-03-18 14:00:08

MongoDB: how to change field type from NumberLong to String?

Collection for example:

db.longs.insert({ "_id" : NumberLong("724756511264022954") });
db.longs.insert({ "_id" : 12477933649175871000 });

You need to turn all _id into strings. Google sent me to stackoverflow , where they offered this option:
db.longs.find().forEach( function (x) {   
  x._id = new String(x._id);
  db.longs.save(x);
});

Then it
db.longs.find()
outputs this thread:
{ "_id" : NumberLong("724756511264022954") }
{ "_id" : 12477933649175871000 }
{ "_id" : 12477933649115871000 }
{ "_id" : { "0" : "N", "1" : "u", "2" : "m", "3" : "b", "4" : "e", "5" : "r", "6" : "L", "7" : "o", "8" : "n", "9" : "g", "10" : "(", "11" : "\"", "12" : "7", "13" : "2", "14" : "4", "15" : "7", "16" : "5", "17" : "6", "18" : "5", "19" : "1", "20" : "1", "21" : "2", "22" : "6", "23" : "4", "24" : "0", "25" : "2", "26" : "2", "27" : "9", "28" : "5", "29" : "4", "30" : "\"", "31" : ")" } }
{ "_id" : { "0" : "1", "1" : "2", "2" : "4", "3" : "7", "4" : "7", "5" : "9", "6" : "3", "7" : "3", "8" : "6", "9" : "4", "10" : "9", "11" : "1", "12" : "7", "13" : "5", "14" : "8", "15" : "7", "16" : "1", "17" : "0", "18" : "0", "19" : "0" } }
{ "_id" : { "0" : "1", "1" : "2", "2" : "4", "3" : "7", "4" : "7", "5" : "9", "6" : "3", "7" : "3", "8" : "6", "9" : "4", "10" : "9", "11" : "1", "12" : "1", "13" : "5", "14" : "8", "15" : "7", "16" : "1", "17" : "0", "18" : "0", "19" : "0" } }

Tried also:
db.longs.find().forEach( function (x) {   
  a = x._id.valueOf().toString();
  x._id = new String(a.valueOf());
  db.longs.save(x);
});

But, almost the same result turned out (except that the letters and special characters from _id disappeared)

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Artem Silantiev, 2016-03-18
@berezuev

db.longs.find().forEach( function (x) {
  var oldId = x._id;
  var updateFlag = false;
  if (typeof x._id == "number") {
    updateFlag = true;
    x._id = x._id+"";
  } else if(x._id instanceof NumberLong) {
    updateFlag = true;
    x._id = x._id.toNumber().toString();
  }
  if (updateFlag) {
    db.longs.save(x);
    db.longs.remove({"_id":oldId});
  }
});

K
Kirill Sadovnik, 2015-01-18
@cyril_sadovnik

All template engines are the same, choose the one whose syntax you like.

S
Sergey, 2015-01-18
Protko @Fesor

Do you need to explain why template engines are needed?
What's the point of using templates?
The difference between handlebars and smarty is that one is for javascript and the other is for php. You will not be able to use handlebars in a php project and vice versa.

R
reedwalter24, 2015-01-18
@reedwalter24

https://github.com/janl/mustache.js

A
Andrey K, 2015-01-18
@mututunus

They have slightly different purposes, HandleBars - used on the client side, Smart - on the server side. So their use can be combined.

Z
Zlatoslav Desyatnikov, 2015-01-18
@pxz

I would recommend jade.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question