Reputation: 251
For example my data is {'abc':'def'},that has a single key-value;
I want this:
do something...//data has been changed to {'abc':'defghi'} or {'abc':'['def','ghi']'}
And I have used this code in nodejs:
var tmp2 = {'userid:location:2013-01-02 15':['092030', '12122.11260E']};
collection.insert(tmp2, {safe:true}, function (err, result) {
var i = 0;
var a = +new Date();
while(i<300000){
tmp2['userid:location:2013-01-02 15'].push(i);
collection.save(tmp2, function () { })
i+=1;
}
var b = +new Date();
console.log(b-a)
});
the save api can replace the same key's value,so use push,I can append data to an exising key's value;
But there are some problems:
Upvotes: 4
Views: 6302
Reputation: 13528
The problem is that you are using save
which overrides the document: last one wins. You should consider using atomic updates with $push
, perhaps via findAndModify
.
Upvotes: 5