在对象数组中添加对象

时间:2018-08-03 22:29:37

标签: arrays object mongoose push

我尝试在对象数组中添加对象(我使用猫鼬)。

我的模特:

let directoryCollection = new Schema(
  {
    email: { type: String },
    directory: [{
        name: { type: String },
        list: [ {type: Schema.ObjectId} ]
    }]
  },
  {collection: 'directory'}
);

我的列表中有一个ObjectID数组。

将代码添加到阵列目录中的对象的代码:

let id = mongoose.Types.ObjectId(req.body.id);

directoryModel.update({
    'email': req.body.email,
}, {
    $push: { 'directory': {
        'name': req.body.directory,
        'list': [ { id } ]
    } 
}
}, function (req, result) {
    console.log(result);
    res.json('ok');
});

但是结果是:

{ ok: 0, n: 0, nModified: 0 }

我的变量是正确的。

我的收藏概述(使用MongoDB Compass):

enter image description here

我认为语法不正确。 但是我找不到正确的语法。

一个主意? 谢谢!

0 个答案:

没有答案
相关问题