javascript - meteor :将子文档添加到现有记录

标签 javascript mongodb meteor

我有以下方案:

dGroup = new SimpleSchema({
    title:      { type: String, optional: true },
    element:    { type: String, optional: true }
});

MongoDB.attachSchema(new SimpleSchema({
    title:      { type: String },
    slug:       { type: String, unique: true },
    language:   { type: String, defaultValue: "en" },
    group:      { type: [dGroup], optional: true },
}));

...在数据库中我得到了这个:

{ "_id" : "ag9qXWpCYm87kZbEk", "title" : "Test", "slug" : "test", "language" : "en" }

现在我想添加一个 dGroup -> title:

updates['group.title'] = 'insert this as a new group title with no element';
MongoDB.update({ _id: Id }, { $push: updates }, function(error) { if(error) console.warn(error); });

但这行不通。所以我需要一些帮助来在 meteor 中添加子文档,以防它们不存在。

最佳答案

首先尝试声明您的对象并正确推送它,如下所示:

var newGroup = {
  title: 'insert this as a new group title with no element'
};
MongoDB.update({ _id: Id }, { $push: {group: newGroup }}, function(error) { if(error) console.warn(error); });

关于javascript - meteor :将子文档添加到现有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087032/

相关文章:

javascript - 每次点击添加按钮添加一个

node.js - Redis 消息的异步操作

javascript - 在每个循环完成渲染集合中的项目后执行代码

meteor - cfs :s3 how to get url after upload?

node.js - 序列化然后反序列化 mongoose.Model 实例

javascript - 添加访问指定FlowRouter路由时的密码提示

javascript - Node.js 安装 : openssl not installed

javascript - 如何创建一个 JS 函数将 `width:100%` 除以 `th` 的数字?

javascript - Angularjs 加载模板很慢

javascript - Mongodb Node.js 通过动态键查找值