eg. 把pmid列由字符型转为整型

1 find + forEach逐条修改

文档较大时会很慢

  1. # 更改全部数据
  2. db.article.find().forEach(function(x) {
  3. x.pmid = NumberInt(x.pmid);
  4. db.article.save(x);
  5. })
  6. # 只更改类型为string的数据
  7. db.article.find({pmid: {$type: 2}}).forEach(function(x) {
  8. x.pmid = NumberInt(x.pmid);
  9. db.article.save(x);
  10. })

2 bulkWrite批量修改

mongo version >= 2.6 and < 3.2

  1. var col = db.article;
  2. var bulk = col.initializeOrderedBulkOp();
  3. var counter = 0;
  4. col.find({ pmid: { $type: "string" }}).forEach(function(data) {
  5. var updoc = {
  6. "$set": {}
  7. };
  8. updoc["$set"]["pmid"] = NumberInt(data.pmid);
  9. // queue the update
  10. bulk.find({
  11. "_id": data._id
  12. }).update(updoc);
  13. counter++;
  14. // Drain and re-initialize every 1000 update statements
  15. if (counter % 1000 == 0) {
  16. bulk.execute();
  17. bulk = col.initializeOrderedBulkOp();
  18. }
  19. });
  20. // Add the rest in the queue
  21. if (counter % 1000 != 0) bulk.execute();

mongo version >= 3.2

  1. var bulkOps = [];
  2. db.article.find({pmid: {$type: 2}}).forEach(function(x) {
  3. var new_pmid = new NumberInt(x.pmid);
  4. bulkOps.push({
  5. "updateOne": {
  6. "filter": {"_id": x._id },
  7. "update": {"$set": {"pmid": new_pmid}}
  8. }
  9. });
  10. })
  11. db.article.bulkWrite(bulkOps, {"ordered": true});
  12. # ============================================================
  13. use pubmed;
  14. var counter = 0;
  15. bulk = [];
  16. db.article.find({pmid: {$type: 2}}).forEach(function(x) {
  17. var new_pmid = new NumberInt(x.pmid);
  18. bulk.push({
  19. updateOne: {
  20. filter: {_id: x._id },
  21. update: {$set: {pmid: new_pmid}}
  22. }
  23. });
  24. counter++;
  25. if (counter % 100000 == 0) {
  26. print(counter, 'records updated');
  27. db.article.bulkWrite(bulk, {ordered: true});
  28. bulk = [];
  29. }
  30. })
  31. if (counter % 100000 != 0) db.article.bulkWrite(bulk, {ordered: true});

3 update方法

Mongo 4.2

  1. db.collection.update(
  2. { a : { $type: 1 } },
  3. [{ $set: { a: { $convert: { input: "$a", to: 2 } } } }],
  4. { multi: true }
  5. )

参考: