【问题标题】:bulk update with Mongoid使用 Mongoid 进行批量更新
【发布时间】:2012-06-20 14:40:15
【问题描述】:

如何使用 Mongoid 进行批量更新?

具体来说,将一个现有字段复制到同一文档的另一个字段。

Product.collection.command("db.products.find(
    {gender : { $ne : null } }
  ).forEach(function(doc) {
    doc.archive_gender = doc.gender;
    doc.gender = null;
    db.products.save(doc);
  })"
)

干杯, 科阿。

【问题讨论】:

标签: ruby mongodb mongoid bulk


【解决方案1】:

好的,所以我们可以 $eval 任何 javascript,但请告诉我更好的解决方案。这就是我能够在 MongoDB 中从一个字段与另一个字段进行批量更新的方式。

db = Mongoid::Config::master
db.command({"$eval" => <<-ENDJS})
  db.products.find({
    gender:{ $ne: null }
  }).forEach(function(doc) {
    doc.archive_gender = doc.gender;
    doc.gender = null;
    db.products.save(doc);
  })
ENDJS

干杯, 科阿。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2012-09-21
    相关资源
    最近更新 更多