1. 程式人生 > >mongodb如何實現更新一個欄位的值為另外一個欄位的值?

mongodb如何實現更新一個欄位的值為另外一個欄位的值?

db.CargoUserProfiles.find().forEach(
 function(item){
   db.CargoUserProfiles.update({"id":item._id},{"$set":{"LastUpdate":item.CreateAt}},false,true)
 }
)

db.CargoUserProfiles.find({}).forEach(function(asset){
    asset.LastUpdate = asset.CreateAt;
})

嘗試了以上兩種方式,都沒成功,其中CreateAt是有資料的欄位,LastUpdate是想新增的欄位

mongodb中id欄位是“_id”而不是“id”

db.CargoUserProfiles.find().forEach(function(item){                 
   db.CargoUserProfiles.update({"_id":item._id},
          {"$set": {"LastUpdate":item.CreateAt}},false,true) 
})