如果我没有回调函数,Mongoose不会更新我的文档

Afs*_*ani 4 mongoose mongodb node.js

这是我的模特:

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 }
});
Run Code Online (Sandbox Code Playgroud)

我尝试用findOneAndUpdate函数更新我的文档:

不起作用:

User.findOneAndUpdate({ github_id: 1 }, { last_github_sync: new Date() }, {});
Run Code Online (Sandbox Code Playgroud)

不起作用:

User.findOneAndUpdate({ github_id: 1 }, { last_github_sync: new Date() });
Run Code Online (Sandbox Code Playgroud)

作品:

User.findOneAndUpdate({ github_id: 1 }, { last_github_sync: new Date() }, {}, function (err, numberAffected, raw) { });
Run Code Online (Sandbox Code Playgroud)

我的意思是我应该绑定回调函数以使更新操作工作,但我不需要那个回调函数,我的代码有什么问题?

Joh*_*yHK 16

请参阅findOneAndUpdate 文档中的示例:

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
Run Code Online (Sandbox Code Playgroud)

如果您不提供回调,它将返回Query您必须调用的对象exec()以执行更新.