Sign In Register

How can we help you today?

Start a new topic
Answered

Full update on document

Hey, next question for nosql is how can I do an update to a full document?


What I want to be able to do is just upload the json for my Monsters metaCollection from my tool (mentioned in another question...working on getting the sdk working with my c# wpf tool)



This is what I was trying, I can get the monsters from the json and just grab the old Id for continuity but I can't make the collection itself save the new data for some reason...I've tried findAndModify, Update and none of them seem to work...

I can't be too far off right? (DB's and nosql is still pretty new, been lots of fumbling about trying to get things to work)


Best Answer

I got it to work like right after I posted haha. Had been staring at it and documentation for too long and missed something

Anyways for future ref, I got it like this:


    var newMonsterData = [];

    newMonsterData = monsterData;

//copy previous ID of document for consistency

    newMonsterData["_id"] = monsters["_id"];


    monsters = newMonsterData;

    monsterCollec.save(monsters);

}

I could probably put it onto like 2 lines but haven't had a chance to go and refactor yet.


1 person has this question

Hey Travis,

So findandmodify is what i would suggest here, but its hard to tell why that isn't working without seeing how you are using it. Maybe you can paste in an example of how you are using it?
But i do see a possible problem here though.
Your 'monsters' variable is a doc in the collection so it would have a JSON format.
But your 'newMonsterData' variable is an array, not a form, so this might do something strange when you try add element to it.
You could try...

var newMonsterData = {};

instead of ...

var newMonsterData = [];

That might work when you try to pass it back into the findandmodifty method.

you could try this....

monsterData._id = monsters._id;
Spark.metaCollection('Monsters').findAndModify(
 { '_id' : monsters._id },
 monsterData
);

and you can skip the new variable completely.
Just check in the test-harness that the correct variables are updating.

Let me know if that makes sense
Sean

 

Answer

I got it to work like right after I posted haha. Had been staring at it and documentation for too long and missed something

Anyways for future ref, I got it like this:


    var newMonsterData = [];

    newMonsterData = monsterData;

//copy previous ID of document for consistency

    newMonsterData["_id"] = monsters["_id"];


    monsters = newMonsterData;

    monsterCollec.save(monsters);

}

I could probably put it onto like 2 lines but haven't had a chance to go and refactor yet.

Good to hear Travis, thanks for the reply

 

Login to post a comment