我有以下架构,并尝试更新answers.counter
中的值。猫鼬位置数组更新
var postSchema = mongoose.Schema({
question : { type: String, required: true },
answers : [answerSchema]
});
var answerSchema = mongoose.Schema({
answe : String,
counter : { type: counterSchema, ref: 'Counter' }
});
var counterSchema = mongoose.Schema({
upvotes : { type: Number, default: 0, min: 0 },
downvotes : { type: Number, default: 0, min: 0 }
});
和代码:
Post.findOneAndUpdate(where, { '$inc': { 'answers.$.counter.upvotes': 1 } }, {'new': true, runValidators: true }, (err, post) => {
if (err) return next(err);
console.log('post=' + JSON.stringify(post));
所以对于where
,我曾尝试以下,其它变型中:
1){ 'answers._id': ObjectId(${req.body.aid}) }
2){ '_id': ObjectId(${req.body.pid}), 'answers._id': ObjectId(${req.body.aid}) }
3){ '_id': ObjectId(${req.body.pid}), 'answers._id': ObjectId(${req.body.aid}) 'answers.counter._id': ObjectId(${req.body.cid}) }
我收到以下错误:
MongoError: exception: The positional operator did not find the match needed from the query. Unexpanded update: answers.$.counter.upvotes
这真是莫名其妙我,因为通过蒙戈控制台,db.posts.update({'answers._id': ObjectId('56ce1376ab820a5b3a149494')}, {$inc: {'answers.$.counter.upvotes': 1}})
不正确地增加正确的价值,开始我这个整个路径上。
在此先感谢任何指针。