node.js - Mongoose - Increment with findOne -
i'm working on query mongoose need skip , limit subdocuments, in same query want increment field in document. query built chaining, because got lot of problem when tried options. have:
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); } }); i increment filed 'views' 1 when calling query, said, tried lot of things , didn't work.
you can use findoneandupdate modify document while fetching one.
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); } });
Comments
Post a Comment