Javascript 嵌套文档上的Mongoose$inc操作错误
我有以下模式:Javascript 嵌套文档上的Mongoose$inc操作错误,javascript,mongoose,mongoose-schema,Javascript,Mongoose,Mongoose Schema,我有以下模式: const postsStatsSchema = new Schema({ postid: { type: String, maxlength: [128, "post ID must be at most 128"], required: true }, likecounter: { type: Number, default: 0, min: 0,
const postsStatsSchema = new Schema({
postid: {
type: String,
maxlength: [128, "post ID must be at most 128"],
required: true
},
likecounter: {
type: Number,
default: 0,
min: 0,
required: true
}
});
const userStatsSchema = new Schema({
username: {
type: String,
required: true,
maxlength: [50, "Name length must be at most 50"]
},
posts: {
type: [postsStatsSchema],
required: true
}
});
const statisticsSchema = new Schema({
month: {
type: Number,
required: true
},
users: {
type: [userStatsSchema],
required: true
}
}, {
timestamps: true
});
我试图增加(或创建,如果文档不存在)postsStats对象中的“likecounter”。我尝试了许多不同的方法,但都没有成功。这是我最后一次尝试:
let update = {};
update['$inc'] = {};
update['$inc'] = {'users.$.username.user.$.posts.somepost.likecounter': 1000};
try {
const res = await stats.findOneAndUpdate({month: 100}, update, {'upsert': true});
console.log(res);
} catch (err) {
console.log(err);
}
我在上面代码中遇到的错误是:
MongoError: Too many positional (i.e. '$') elements found in path
我尝试了许多变化,有“$”符号和没有“$”符号,但只设法增加了月份。我相信是嵌套文档出了问题,但就是搞不清楚它是什么。
users.$。username
被定义为字符串。您希望users.$.username.user
成为什么?users.$.username
被定义为字符串。你期望用户.$.username.user
是什么?