node.js - Mongoose upsert duplicate key error -


i'm trying upsert using mongoose, i'm getting duplicate key error key should trigger upsert.

schema:

"resource": {type: schema.objectid, ref: "resource"},                        "skill": {type: schema.objectid, ref: "skill"},                              "level": {type: number, min: 1, max: 5}  .index({skill: 1, resource: 1}, {unique: true}); 

then make call:

    //self resource instance     resourceskilllevel.update({                                                    resource: self._id,         skill: skill._id,         level: level     }, {$set: {level: level}}, {upsert: true}, cb); 

if (resource, skill) not exist, call works fine , creates resourceskilllevel entry. however, when call again duplicate key error index. listed duplicate key tuple resource/skill key. why isn't upserting when finds duplicate?

you're including level in update query criteria parameter, if doesn't match existing record try , create new doc fail unique index spans skill , resource.

try changing update this:

resourceskilllevel.update({                                                resource: self._id,     skill: skill._id }, {$set: {level: level}}, {upsert: true}, cb); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -