Meteor Mongo - upsert and $inc with aldeed simple

2019-07-25 01:51发布

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).

This works perfectly without the Schema:

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

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

The Schema:

Local.Viewed.Schema = new SimpleSchema({
    imageId: {
        type: String
    },
    imageName: {
        type: String
    },
    timesViewed: {
        type: Number,
        defaultValue: 0
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date();
        }
    }
});

Problem:

When i use this Schema i get an error:

update failed: Error: Times viewed is required at getErrorObject

The Schema seems to be demanding that 'timesViewed' is set. I tried using 'defaultValue: 0' in the Schema but that doesn't insert a default Value of 0.

Question: How can i make the Schema compatible with this query?

Thanks for your help!

Muff

2条回答
我想做一个坏孩纸
2楼-- · 2019-07-25 02:31

Ok i played around a little based on your suggestions and the previous thread and this solution works without errors and expected results:

Schema:

Data = {};
Data.Viewed = new Mongo.Collection("dataViewed", {});
Data.Viewed.Schema = new SimpleSchema({
    imageId: {
        type: String
    },
    userId: {
        type: String,
        autoValue: function() {
            return this.userId;
        }
    },
    imageName: {
        type: String
    },
    timesViewed: {
        type: Number
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date();
        }
    }
});
Data.Viewed.attachSchema(Data.Viewed.Schema);

Method:

Meteor.methods({
    dataViewed(obj) {
        Data.Viewed.upsert({
            imageId: obj._id,
            userId: this.userId
        }, {
            $setOnInsert: {
                imageId: obj._id,
                userId: this.userId,
                imageName: obj.term,
                timesViewed: 0
            },
            $inc: {
                timesViewed: 1
            }
        });
    }
});

I think the problem was that i defined an defaultValue/ autoValue in the Schema for 'timesViewed'. Further every property that is mentioned in the schema must be mentioned in the $set or $setOninsert command.

Thanks for your help!

查看更多
走好不送
3楼-- · 2019-07-25 02:32

have you tried

$setOnInsert: {
    imageId: this._id,
    imageName: this.name,
    timesViewed: 0
},
查看更多
登录 后发表回答