Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嵌套文档上的Mongoose$inc操作错误_Javascript_Mongoose_Mongoose Schema - Fatal编程技术网

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
是什么?