0

I have an image gallery with a forward and backward-button. on a click on either of the buttons i want to upsert an entry in the local database with the times the image has been viewed (so i can later see which image has been viewed the most).

When i use:

'click .btn-forward, click .btn-backward' (event, template) {

Local.Viewed.upsert({
        imageId: this._id
    }, {
        $setOnInsert: {
            imageId: this._id,
            imageName: this.name,
            timesViewed: 0
        },
        $inc: {
            timesViewed: 1
        }
    });

}

Problem: 'timesViewed' does only increase on the insertion in the database.

Question: How can i make this query increase the value of 'timesViewed' with every click event?

Thanks for your help!

Muff

Raggamuffin
  • 699
  • 1
  • 6
  • 19

1 Answers1

1

Remove timesViewed from $setOnInsert

Alex Polkhovsky
  • 3,340
  • 5
  • 29
  • 37
  • Do you know a way i can make this compatible with aldeed simple schema? This demands all fields set when using $set or $setOnInsert – Raggamuffin Apr 12 '16 at 13:22
  • Sorry, I don't know. I haven't used aldeed simple schema. You may want to create another question on SO to find help with that. – Alex Polkhovsky Apr 12 '16 at 13:26
  • See: [thread sequel](http://stackoverflow.com/questions/36575144/meteor-mongo-upsert-and-inc-with-aldeed-simple-schema-update-failed) – Raggamuffin Apr 12 '16 at 13:44