小编use*_*205的帖子

mongodb 更新推送数组

我有以下架构。我在 mongodb 中使用 node.js

attributes: {
    type: { type: 'string' },
    title: { type:'string' },
    description: { type:'string' },
    is_active: { type:'boolean',defaultsTo:true },
    createdBy: { type:'json' },
    attachments:{ type:'array' }
}

arr = [{
    'id':attResult.id,
    'subtype':type,
    'title'  : attResult.title,
    'body'  : attResult.body,
    'filetype' : attResult.filetype
}];
Run Code Online (Sandbox Code Playgroud)

我正在尝试将附件推送到文档唯一的“附件”数组中。

这是我的查询。

books.update(
    { id: refid },
    { $push: { attachments: arr } }
).done(function (err, updElem) {
    console.log("updElem" + JSON.stringify(updElem));     
});
Run Code Online (Sandbox Code Playgroud)

我的查询有什么问题,没有错误但没有更新附件。

我希望我的结果是这样的:

{ 
    "_id" : 5,
    "attachments": [
        { 
            "id": "xxxxxxx",
            "subtype": …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js sails.js

3
推荐指数
2
解决办法
2万
查看次数

标签 统计

mongodb ×1

node.js ×1

sails.js ×1