Targeted Version: 5.1.6
This will update the document on every call. Not the best option however it is atomic.
Ref: findOneAndUpdate
const Model = require('model')
const options = {
new: true,
setDefaultsOnInsert: true,
runValidators: true,
upsert: true
}
function findOrCreate (query, update) {
return Model.findOneAndUpdate(query, update, options)
}
Returns a document if found and updates a views
field.
Ref: findOneAndUpdate
const Model = require('model')
const update = { $inc: { views: 1 } }
function findAndIncrement (_id) {
return md.Activity.findOneAndUpdate({ _id }, update)
}