Mongoimport csv files with string _id and upsert

后端 未结 5 1996
清歌不尽
清歌不尽 2021-01-13 05:26

I\'m trying to use mongoimport to upsert data with string values in _id. Since the ids look like integers (even though they\'re in quotes), mongoimport treats them as intege

5条回答
  •  醉梦人生
    2021-01-13 06:10

    As an alternative to @Jesse, you can do something similar in the mongo console, e.g.

    db.my_collection.find().forEach(function (obj) {
      db.my_collection.remove({_id: obj._id); // remove the old one
      obj._id = '' + obj._id; // change to string
      db.my_collection.save(obj); // resave
    });
    

    For non _id fields you can simply do:

    db.my_collection.find().forEach(function (obj) {
      obj.someField = '' + obj.someField; // change to string
      db.my_collection.save(obj); // resave
    });
    

提交回复
热议问题