MongoDB update collection using reference collection value

Scenario:
Want to update the field_a1 of collection a with the value of filed_b1 from collection b using connection key field_a2 = filed_b2 with collection a have field flag is set.

Solution:(using javascript in Mongo Shell)
db.a.find({flag:{$exists: true}}).forEach(
  function (item) {
    item.field_a1 = db.b.findOne({field_b2: item.field_a2}).field_b1;
    db.a.save(item);
  }
)


Enjoy it!

猜你喜欢

转载自belmount.iteye.com/blog/1595239
今日推荐