Meteor & Mongo: addToSet inserting -
i have documents in base:
//example docs {"_id": "qwerty12345", "name": "bob", "cards":["cardid1", "cardid2", "cardid3"]}
i'm using inserting data:
template.insert.events({ 'click add': function(){ if(confirm("add card?")); mycollection.update({_id: session.get('fooid')}, { $addtoset: { cards: this._id}}) } });
then i'm using helper template:
template.index.helpers({ cards: function(){ query = mycollection.findone({_id: session.get('fooid')}); return query.cards; } });
and in template:
<img src="{{img}}" class="add"> {{#each cards}} {{this}}<br> {{/each}}
this works perfecty, have trouble:
as see, every image have id , url({{image}}), i'm need add image url 'mycollection' every card(on click).
how make it?
and second problem: how allow mongo insert duplicates "cards" array?
do mean every card has id , image field? guess so.
you can add nested object array fields.
mycollection.update({_id: session.get('fooid')}, { $addtoset: { cards: {id: this._id, image: this.image}}})
.
in template: {{#each cards}} {{this.id}}: {{this.image}}<br> {{/each}}
for second problem: can use $push
instead of $addtoset
Comments
Post a Comment