mongoose更新没有callback

我有一个典型的模式和模型:

var mongoose = require('mongoose'); var userSchema = new mongoose.Schema({ email: String, password: String, profile: { name: String, surname: String, photo: String }, stats: { lastLogin: { type: Date, default: Date.now }, loginCount: Number, lastIP: String }, source: String, deleted: Boolean, dateCreated: { type: Date, default: Date.now } }); mongoose.model('User', userSchema); 

当我执行这个更新时,它只在我定义callback时才起作用,否则它只是执行,但是在数据库中没有值的改变:

 User.update({email:'foo@bar.com'}, {$inc: {'stats.loginCount': 1}}); 

这工作:

 User.update({email:'foo@bar.com'}, {$inc: {'stats.loginCount': 1}}, function() {}); 

这是一个错误? 我没有看到在文档中是否需要callback,但这很奇怪,我想我在这里错过了一些东西。

注:我通过电子邮件进行testing提议匹配,我在使用简单的Express v3.0.6安装程序在NodeJS v0.8.17中使用mongoose v3.5.4。

提前致谢。

用mongooseupdate的正确方法如下:

 User.update(query, update).exec(callback); 

这样你就可以跳过callback

 User.update(query, update).exec(); 

当你打电话

 User.update(query, update) 

它返回一个查询对象 。

在查询数据库时非常有用,因为在执行查询对象之前可以使用查询对象进行操作。 例如,您可以为查询查询指定一个limit

 User.find(query).limit(12).exec(callback); 

Update使用相同的机制,虽然它不那么有用。