Meteor:将子文档添加到现有记录

时间:2015-08-19 05:16:38

标签: 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 },
}));

......在DB中我得到了这个:

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

现在我要添加一个dGroup - >标题:

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); });

但这不起作用。所以我需要一些帮助来在流星中添加子文档,以防它们不存在。

1 个答案:

答案 0 :(得分:1)

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

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); });