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.

update with upsert: true doesn't work sometimes

See original GitHub issue

I need to keep only one document in my nedb.

So I make db.update({ active: true }, data, { upsert: true }, function (err, savedNum) { ... }) where data is { active: true, name: 'name', token: '1234567890' }

It saves doc and keep it only one yet on a first time. But second time while updating (data is almost the same, only token is different), it doesn’t replace existing document - it adds another one…

On the third time it stops to add new ones, but replace one of existing ones…

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
louischatriotcommented, Sep 2, 2013

@voronianski this is not a bug. The datafile is append only but gets autocompacted upon every reload. If you look closely, you’ll see that the documents in your file habe the same _id, meaning they re in fact the different states of the same doc, the last one being the latest one

When checking the contents of a db, never look at the file, always do a db.find()

0reactions
voronianskicommented, Sep 2, 2013

@louischatriot thanks, didn’t notice it 😃 panic without reason 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongodb update with upsert:true does not act as in insert
As it turns out, it works only when I use insert , if upsert:true is provided with the update, the new inserted doc...
Read more >
UpdateMany, Upsert does not perform insert - MongoDB
If an upsert operation results in an insert, the query must include an exact equality match on the array field in order to...
Read more >
Problem Solving: Mongoose Validators Don't Run on Update ...
If the upsert flag is true, we update the existing documents and create the ones that don't. If the document doesn't exist, we...
Read more >
Upsert in SQL: What is an upsert, and when should you use ...
If the employee ID exists in the table, update that row with the new information. · If the employee ID doesn't exist in...
Read more >
Collections | Meteor API Docs
When you write to the database on the client ( insert , update , remove ) ... It works just like any other...
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