猫鼬推入嵌套数组

时间:2018-12-14 23:06:33

标签: javascript mongodb mongoose

我有一个这样的架构:

const TeachSchema = mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true },
  username: { type: String, required: true },
  password: { type: String, required: true },
  created: {type: Date, default: Date.now},
  classes: [
    {
      cName: String,
      student: [
        {
          name: { type: String, required: true },
          lname: { type: String, required: true },
          gender: { type: String, enum: ['M', 'F'] },
          grades: { type: Array }
        }
      ]
    }]
});

我希望在班级->学生->为ID(req.params.id)找到的用户的成绩中添加成绩(投票,数字)。

因此,象形用户“ Test”的结果对象将是:

{
        "_id" : ObjectId("5c0c55642440311ff0353846"),
        "name" : "Test",
        "email" : "test@gmail.com",
        "username" : "test",
        "password" : "$2a$10$RftzGtgM.DqIiaSvH4LqOO6RnLgQfLY3nk7UIAH4OAvvxo0ZMSaHu",
        "created" : ISODate("2018-12-08T23:36:04.464Z"),
        "classes" : [
                {
                        "_id" : ObjectId("5c0c556e2440311ff0353847"),
                        "cName" : "1A",
                        "student" : [
                                {
                                        "grades" : [
                                                5 //added value (variable 'voto')
                                        ],
                                        "_id" : ObjectId("5c0c55812440311ff0353848"),
                                        "name" : "StudentName",
                                        "lname" : "StudenteLastName",
                                        "gender" : "M"
                                }
                        ]
                }
        ],
        "__v" : 0
}

在MongoDB中,我可以通过以下查询添加该值:

db.teachers.update({"_id": ObjectId("5c0c55642440311ff0353846"), "classes._id": ObjectId("5c0c556e2440311ff0353847"), "classes.student._id": ObjectId("5c0c55812440311ff0353848")},{$addToSet: {"classes.$.student.grades":6}})

我要问的是如何在Express的Mongoose中做同样的事情。 我尝试过的代码(以及许多其他查询)是(注意:变量“ class”是classID):

Teacher.findByIdAndUpdate(
    {_id: req.user.id, 
    "classes": {$elemMatch: {_id: classe}},
    "classes.student": {$elemMatch: {_id: req.params.id}}
    },
    {$addToSet: {"classes.$.student.0.grades": voto}},
    (err, result) => { 
        if (err) {console.log(err)}
        else {console.log(result); res.json(result)}
    }
)

和:

Teacher.findByIdAndUpdate(
    {_id: req.user.id, 
    "classes": {$elemMatch: {_id: classe}},
    "classes.student": {$elemMatch: {_id: req.params.id}}
    },
    {$push: {"classes.0.student.0.grades": voto}}
    (err, result) => { 
        if (err) {console.log(err)}
        else {console.log(result); res.json(result)}
    }
)

但是我得到-> errmsg:位置运算符未从查询中找到所需的匹配项;在第二个变量中,我可以添加值,但总是在我的学生数组的第一个学生中添加,我需要添加到通过ID找到的特定用户。

我尝试遵循this问题上显示的指示,但是得到(控制台记录结果)“ [n:1修改:0 ok:1]”,因此我知道我尝试过的查询找到了该学生但没有将值推入“成绩”数组中。 出现上述错误的代码:

 Teacher.update(
        {_id: req.user.id, 
        "classes": {
            "$elemMatch": {
                "_id": req.body.classe, "student._id":req.params.id
            }
        }},
        {
            $push: {"classes.$[outer].student.$[inner].grades": vote}

        },
        {
            "arrayFilters": [{"outer._id": req.body.classe}, {"inner._id": req.params.id}],
        },
        (err, doc) => {
            if (err) {console.log(err)}
            else { console.log(doc); res.json(doc)}
        }
    )

所以我不能这样做。任何帮助表示赞赏!

0 个答案:

没有答案