Upserting keeps adding duplicates of the same record to the database

See the code below, upserting the same record three times in a row:

var Nedb = require('nedb');
var db = new Nedb({ filename: 'test.db' });
db.loadDatabase(function (err) { 
    db.update({ '_id': 'foo_id' }, { somedata: 'hello' }, { 'upsert': true }, function (err, numReplaced) {
        console.log(numReplaced, err);
        db.update({ '_id': 'foo_id' }, { somedata: 'hello' }, { 'upsert': true }, function (err, numReplaced) {
            console.log(numReplaced, err);
            db.update({ '_id': 'foo_id' }, { somedata: 'hello' }, { 'upsert': true }, function (err, numReplaced) {
                console.log(numReplaced, err);

            });
        });
    });
});

At the end of execution, test.db is:

{"somedata":"hello","_id":"foo_id"}
{"somedata":"hello","_id":"foo_id"}
{"somedata":"hello","_id":"foo_id"}

I’m on Mac OS Yosemite (10.10.1) running nodejs 0.10.33 and nedb 0.11.1. This issue goes well with the other one I found some time ago #217 . Thanks!

Giacecco

3 thoughts on “Upserting keeps adding duplicates of the same record to the database

  1. @louischatriot I come accross to this problem also, it would be better to make a notice in a subsection of update/delete section. no one reads the whole readme fro a simple update documentation. Thanks!

  2. Whenever I upsert and do not have records the user_upsert is undefined do you know why?
    // Upserting a document
    users.update({ id: user_data.id }, user_data, { upsert: true }, function (err, numReplaced, user_upsert, affected) {
    console.log(numReplaced, user_upsert, affected);
    // this will be 1, undefined, undefined even when there is the document
    });

    users.persistence.compactDatafile();