question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upserting keeps adding duplicates of the same record to the database

See original GitHub issue

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 https://github.com/louischatriot/nedb/issues/217 . Thanks!

Giacecco

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
mtrabelsicommented, Jul 10, 2015

@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!

5reactions
louischatriotcommented, Nov 30, 2014

This is the expected bahavior, please read the readme before asking about sutff in it …

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Avoid Inserting Duplicate Records in SQL INSERT ...
This article discusses inserting records from another table or tables using an INSERT INTO SELECT statement without duplicate key errors.
Read more >
Why does upsert cause a DUPLICATE_VALUE error?
Because, both the records that you are inserting are having same name ... any duplicates so it goes ahead with the INSERT and...
Read more >
Duplicate records are created during upsert in Apex batch job.
I need to extract records from an external application database table and Create a new table (custom object named CustomObj) in Salesforce ...
Read more >
One row, many threads: How to avoid database duplicates in ...
We talk about PostgreSQL, but the same idea applies to MySQL and other SQL databases. To create a new uniqueness constraint with an...
Read more >
How To Avoid Inserting Duplicate Records in MySQL - Ubiq BI
If we use INSERT statement to add data with duplicate rows, MySQL will throw an error and stop query execution after the first...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found