mongoose – 用findOne增加

我正在使用Mongoose查询一些查询,我需要跳过和限制子文档,但在同一个查询中,我想增加文档中的某个字段。 目前我的查询是用链接构build的,因为当我试图用选项来完成时,我遇到了很多问题。 这是我的:

Model.findOne({ shorten_id: id }).select('title content comments views').slice('comments', [0, 10]).exec(function(err, db_res) { if (err) { throw err; } else { console.log(db_res); } }); 

我想在调用这个查询时增加1的“意见”,但正如我所说的,我尝试了很多东西,但它不工作。

您可以使用findOneAndUpdate来修改文档,同时也可以获取文档。

 Model.findOneAndUpdate({ shorten_id: id }, { $inc: { fieldToIncrement: 1 }) .select('title content comments views') .slice('comments', [0, 10]) .exec(function(err, db_res) { if (err) { throw err; } else { console.log(db_res); } });