【发布时间】:2013-09-18 19:59:46
【问题描述】:
我有一个包含嵌入式文档数组的模型。
var CourseSchema = mongoose.Schema({
invitations: [InvitationSchema],
total:Number
});
var InvitationSchema = new mongoose.Schema({
token: { type:String, required: true},
email: String
});
想通过匹配请求中邀请嵌入数组的令牌来更新/增加邀请总数,这里我按照http://diogogmt.wordpress.com/2012/03/23/update-elementmatch-and-the-positional-operator-on-mongodbmongoose/中的教程进行操作
所以我尝试了
var options = { new: false , select:'_id'};
CourseModel.update({'_id':id,'invitations':{'$elemMatch':{'token':token}}},{'$inc':{'total':1}},options,function(err,data) {
if(err){callback(err, null);}
else{
callback(null, data);
}
})
但它不起作用,如果我删除 'invitations':{'$elemMatch':{'token':token}},查询将起作用
【问题讨论】:
-
我试过时效果很好。什么对你正在尝试的东西不起作用?
-
我在 mongo shell 上试过,也可以。我猜猫鼬由于某种原因不起作用。我正在检查现在是什么问题