Sign In Register

How can we help you today?

Start a new topic
Answered

duplicate key error when using update

 hi,


I want to store 5 set of stats for each user. Basicly they give me some values and a number and it gets stored online


Here is the cloudcode:

 

var playerID = Spark.getPlayer().getPlayerId();

var hp = Spark.getData().hp;
var dmg = Spark.getData().dmg;
var cdr = Spark.getData().cdr;
var setbonus = Spark.getData().setbonus;
var setID = Spark.getData().nr;
var ms = Spark.getData().ms;

var wholeSetBonus = {
    
    "nr": setID,
    "hp": hp,
    "dmg": dmg,
    "cdr": cdr,
    "ms": ms,
    "setbonus": setbonus
    
}

var dataCol = Spark.runtimeCollection("sets");

   dataCol.update({
        "_id": playerID,
        "nr": setID
    }, 
    {
        "$set": wholeSetBonus
    }, 
    true,
    false
    );

 

And here is the error:

"message": "Write failed with error code 11000 and error message 'E11000 duplicate key error index: 304283-game-preview.script.sets.$_id_ dup key: { : \"5835639ab5973404b3f4b1ca\" }' (event/setStarsets.js#31)"


When _id & nr NOT FOUND in DB "wholeSetbonus" is stored correctly.

When _id & nr FOUND in the DB "wholeSetbonus" is updated correctly.

When _id is FOUND, but nr is NOT FOUND, i get the error message.


I thought that update with $set creates a new db entry when its not found.



Best Answer

Hi Kelvin


GameSparks uses MongoDB for our Database's.

The problem is the _id is a unique key given by MongoDB for each document.

You cannot set the _id it is auto generated by MongoDB

You could add a key called something else like player_id to store the playerId.


Regards

Katie 


Answer

Hi Kelvin


GameSparks uses MongoDB for our Database's.

The problem is the _id is a unique key given by MongoDB for each document.

You cannot set the _id it is auto generated by MongoDB

You could add a key called something else like player_id to store the playerId.


Regards

Katie 


1 person likes this
I figured this out too after searching for mongoDB update.

Thanks for your answer anyway, i hope others see this, as this "error" happened to others aswell.

 

Login to post a comment