【发布时间】:2014-04-11 09:25:04
【问题描述】:
我有一个这样的架构:
var ClassSchema = new Schema({
name: String,
kids: [{ type: Schema.Types.ObjectId, ref: 'Kid' }],
kidsCount: { type: Number, default: 0}
});
现在我想pull 数组中的一个 Kid 并减少计数器,前提是该 Kid 存在于数组中。可以在一个命令中完成吗?
我试过这个:
Class.findByIdAndUpdate(
{ _id:classId, kids:kidId },
{ $pull:{ kids:kidId }, $inc:{ kidsCount:-1 } },
function(err, updatedClass){
// ...
}
);
但无论是否找到 Kid,kidsCount 总是递减。
【问题讨论】:
标签: arrays node.js mongodb mongoose