This is my model:
var UserSchema = new Schema({
username: { type: String, unique: true, index: true },
url: { type: String },
name: { type: String },
github_id: { type: Number },
avatar_url: { type: String },
location: { type: String },
email: { type: String },
blog: { type: String },
public_repos: { type: Number },
public_gists: { type: Number },
last_github_sync: { type: Date },
created_at: { type: Date, default: Date.now }
});
I try to update my document with findOneAndUpdate
function:
Doesn't work:
User.findOneAndUpdate({ github_id: 1 }, { last_github_sync: new Date() }, {});
Doesn't work:
User.findOneAndUpdate({ github_id: 1 }, { last_github_sync: new Date() });
Works:
User.findOneAndUpdate({ github_id: 1 }, { last_github_sync: new Date() }, {}, function (err, numberAffected, raw) { });
I mean I should bind callback function to make the update operation work but I don't need that callback function, what's the problem with my code?
See the examples in the findOneAndUpdate
documentation:
A.findOneAndUpdate(conditions, update, options, callback) // executes
A.findOneAndUpdate(conditions, update, options) // returns Query
A.findOneAndUpdate(conditions, update, callback) // executes
A.findOneAndUpdate(conditions, update) // returns Query
A.findOneAndUpdate() // returns Query
If you don't provide a callback, it returns a Query
object that you must call exec()
on to execute the update.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With