使用聚合更改集合中记录的值
Posted
技术标签:
【中文标题】使用聚合更改集合中记录的值【英文标题】:Change value for a records in collection using aggregation 【发布时间】:2020-05-08 08:05:17 【问题描述】:有一个这样的集合,如何在不修改其他值的情况下将特定值从 true 更改为失败
_id:ObjectId('......')
ton_id :ObjectId('abcd')
value:true
date:2018-1-13T07:09:40.075+00:00
_id:ObjectId('......')
ton_id :ObjectId('abcd1')
value:no result
date:2018-12-13T07:09:40.075+00:00
将值从 true 更改为失败,仅使用聚合
预期输出:
_id:ObjectId('......')
ton_id :ObjectId('abcd')
value:fail
date:2018-1-13T07:09:40.075+00:00
_id:ObjectId('......')
ton_id :ObjectId('abcd1')
value:no result
date:2018-12-13T07:09:40.075+00:00
【问题讨论】:
【参考方案1】:查看这个答案update aggregate results in mongodb?
您是否特别想使用聚合而不是 findOneAndUpdate?因为这可以很容易地实现,我认为可以通过在 for 循环中执行以下操作来实现。
module.exports =
async updateGCGuideSent()
const mongoose = require('mongoose');
const Booking = mongoose.model('Booking');
findObject = remindGuides: false;
update = remindGuides: true;
await Booking.findOneAndUpdate(findObject, update, new: true, (err, doc) =>
console.log(update);
);
,
;
【讨论】:
好的,您是否检查了链接中的最佳答案?可能会有所帮助。以上是关于使用聚合更改集合中记录的值的主要内容,如果未能解决你的问题,请参考以下文章