我在请求某个接口的时候需要里面的num
字段自增怎么实现呢?
这种方式查询有些表就关联不了,但能实现自增
model.findByIdAndUpdate({_id:Id},{ $inc:{ num: 1 } },{new:true}).populate('xxx');
用这种方式更方便关联
model.aggregate([
{ $lookup:{
from:'xxx', // 关联表
localField: "xxx", // 本表id
foreignField:'_id', // 被关联表 id
as: "xxx",
}},
{
$match:{
_id: mongoose.Types.ObjectId(Id),
}
}
])
目前想要通过aggregate
查询的时候num
自增。
采用aggregate时,通过$lookup建立的是一个临时的collection,类似于view,不能通过这个临时的collection反向去修改原始的collection中的数据。
这个情况下还是要把查询和修改分开