Meteor 流星及;Mongo:addToSet插入

Meteor 流星及;Mongo:addToSet插入,meteor,Meteor,我的数据库中有一些文档: //example docs {"_id": "qwerty12345", "name": "Bob", "cards":["cardId1", "cardId2", "cardId3"]} 我用它来插入数据: Template.insert.events({ 'click add': function(){ if(confirm("Add card?")); mycollection.update({_id: Session.

我的数据库中有一些文档:

//example docs
{"_id": "qwerty12345", "name": "Bob", "cards":["cardId1", "cardId2", "cardId3"]}
我用它来插入数据:

Template.insert.events({
    'click add': function(){
        if(confirm("Add card?"));
        mycollection.update({_id: Session.get('fooId')}, { $addToSet: { cards: this._id}})

    }
});
然后我将此帮助器用于我的模板:

Template.index.helpers({
  cards: function(){
        query = mycollection.findOne({_id: Session.get('fooId')});
        return query.cards;
    }
});
<img src="{{img}}" class="add">
{{#each cards}}
{{this}}<br>
{{/each}}
在模板中:

Template.index.helpers({
  cards: function(){
        query = mycollection.findOne({_id: Session.get('fooId')});
        return query.cards;
    }
});
<img src="{{img}}" class="add">
{{#each cards}}
{{this}}<br>
{{/each}}

{{{#每张卡}
{{this}}}
{{/每个}}
这很好用,但我有个问题:

正如您所看到的,每个图像都有id和url({image}),我需要为每个卡(单击时)将图像url添加到“mycollection”

怎么做

第二个问题是:
如何允许mongo向“cards”数组插入副本?

您的意思是每张卡都有id和图像字段吗?我想是的

可以将嵌套对象添加到数组字段中。那样

mycollection.update({u-id:Session.get('fooId')},{$addToSet:{cards:{id:this.\u-id,image:this.image}}})

在模板中:

{{{#每张卡}
{{this.id}}:{{this.image}}
{{/每个}}

对于第二个问题:您可以使用
$push
而不是
$addToSet