Can't update the MongoDB collection

196 Views Asked by At

When I try to update the collection with the following code:

Drawings.update({_id: this._id}, {$push: {"votedUsers": Meteor.userId()}}, function(error, doc) {
            if(error){
                console.log(error);
            } else {
                console.log("Success");
            }
        });

I get an error about conflicting paths,

The shorter version of the error is below:

update failed: MongoError: Updating the path 'votedUsers' would create a conflict at 'votedUsers'

I longer version of the error:

> details: undefined
> error: 409
> errorType: "Meteor.Error"
> isClientSafe: true
> message: "MongoError: Updating the path 'votedUsers' would create a conflict at 'votedUsers' [409]"
> reason: "MongoError: Updating the path 'votedUsers' would create a conflict at 'votedUsers'"
> stack: "Error: MongoError: Updating the path 'votedUsers' would create a conflict at 'votedUsers' [409]↵    at Connection._livedata_result (http://localhost:3000/packages/ddp-client.js?hash=5333e09ab08c9651b0cc016f95813ab4ce075f37:1796:23)↵    at Connection.onMessage

What is causing the issue?

UPDATE:

I am using aldeed:collection2 package to create the collection and below is how I am configuring it:

Drawings = new Mongo.Collection("drawings");

Drawings.allow({
    insert: function(userId, doc){
        return !!userId;
    },
    update: function(userId, doc){
        return !!userId;
    }
});

drawingSchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 200
    },
    authorId: {
        type: String,
        label: "Author",
        autoValue: function() {
            return this.userId;
        }
    },
    author: {
        type: String,
        autoValue: function(){
            return Meteor.user().username;
        }
    },
    description: {
        type: String,
        label: "Brief summary",
        optional: true,
        max: 1000
    },
    votes: {
        type: Number,
        autoValue: function(){
            return 0;
        }
    },
    votedUsers: {
        type: [String],
        autoValue: function(){
            return [""];
        }
    },
    url: {
        type: String,
        optional: false
    },

    createdAt: {
        type: Date,
        label: "Created At",
        autoValue: function() {
            return new Date();
        }
    }
});

Drawings.attachSchema(drawingSchema);

this is how the collection looks inside mongo shell

0

There are 0 best solutions below