通过mongoose将值推送到嵌套数组对象

时间:2015-08-22 14:26:04

标签: javascript node.js mongodb mongoose

我有以下代码:

select t1.* from
(
select t.keycol,min(t.recorded_at) as recorded_at from data as t inner join
lookup_table as dates on t.recorded_at>look.recorded_at
group by t.keycol
) as t2 on t1.keycol=t2.keycol and t1.recorded_at=t2.recorded_at

我想将主题名称推送到lesson.units.topic。当我继续按下投影时:

var query = {
    "lessons.teacher": req.user._id,
    "lessons.units.name": req.body.unitName
};

var projection = {
    $push: {
        "lessons.0.units.$.topics": {
            "name": req.body.topicName
        }
    }
};

Class.findOneAndUpdate(query, projection, function (err, class) {
    if (err) throw err;

    res.redirect('/topics');
});

它总是会添加到第一课程数组,但是我想把它推到课程数组中,以下查询满足要求:

"lessons.0.units.$.topics": {
    "name": req.body.topicName
}

你能帮我理解我能做些什么来解决这个问题吗? 我尝试过使用以下投影:

"lessons.units.name": req.body.unitName

但我不能使用多个var projection = { $push: { "lessons.$.units.$.topics": { "name": req.body.topicName } } };

0 个答案:

没有答案