Sign In Register

How can we help you today?

Start a new topic
Answered

MongoCollection modify/add json field

 

Hi ,

I need to get a certain document and modify only one field in it .

Tried to use find and modify as fellow :

var success = playerSavesCollection.findAndModify({"playerName" : Spark.getPlayer().getUserName()},JSONData);


but it's replacing everything in this document with the new jsondata when i want to only add the jsondata if not existing or modify it if exist


Thanks :D

Best Answer

Hello,


You can simply do this:

 

var playerSaveData =  playerSavesCollection.findOne({"playerName" : Spark.getPlayer().getUserName()});
if (playerSaveData !== null)
{
    playerSaveData.anyField = "anyValue";
    var success = challengeCollection.update({"playerName" : Spark.getPlayer().getUserName()}, playerSaveData);
}

 


Answer

Hello,


You can simply do this:

 

var playerSaveData =  playerSavesCollection.findOne({"playerName" : Spark.getPlayer().getUserName()});
if (playerSaveData !== null)
{
    playerSaveData.anyField = "anyValue";
    var success = challengeCollection.update({"playerName" : Spark.getPlayer().getUserName()}, playerSaveData);
}

 

Nice , Thanks for your help :D