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.

push() and save() leaks it's query on the next save() as well

See original GitHub issue

Overview

When saving() an operation, queries from a previous operation that was already saved appear in the next save() query as well

What I do

I call the below method (in rapid succession) with msg.action flags that fall:

  • The "newBoard" case, which simply push-es an item into an array
    • I then save()
  • The switchBoard case, which simply toggles a value, that lies outside of the array
    • I then save()

I’m expecting to see mongoose sending 2 distinct queries:

  • A push query to push the item into the this.boardBucket.items Array
  • A set query to set the value boardBucket.currentBoardId

The problem

Mongoose sends 2 queries but both contain the pushAll command

Why does this happen? How can I ameliorate this?

The code

This is the method that get’s called which does the operations:

roomSchema.methods.applyOperation = function(msg, options) {
  options = options || {};

  // Adding the below `console.log` to confirm in the logs that this function
  // is indeed called only *twice*
  console.log("Operation " + msg.action);

  switch (msg.action) {
    case "setBoardBucket":
      this.set("boardBucket", msg.msg.boardBucket);
      break;
    case "newBoard":
      this.boardBucket.items.push(msg.msg.board);
      break;
    case "switchBoard":
      this.set("boardBucket.currentBoardId", msg.msg.boardId);
      break;
  }

  this.save((err, result) => {
    if (err) console.log(err);
  })
}

The Mongoose logs:

As you can see there are 2 distinct queries with both containing a $pushAll command

Operation newBoard
Mongoose: rooms.update({ _id: ObjectId("57b5c537e88db17f17012578") }, { '$pushAll': { 'boardBucket.items': [ { boardId: '147705812266', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b5c7c28d08643f18640332"), undoedItems: [], boardItems: [] } ] } })
Operation switchBoard
Mongoose: rooms.update({ _id: ObjectId("57b5c537e88db17f17012578") }, { '$set': { 'boardBucket.currentBoardId': '147705812266' }, '$pushAll': { 'boardBucket.items': [ { boardId: '147705812266', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b5c7c28d08643f18640332"), undoedItems: [], boardItems: [] } ] } })

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Aug 23, 2016

Yes you should. Mongoose does track changes, but it keeps the changes until the document has been successfully saved (in other words, if an error occurs in save, mongoose still retains all the changes so you can retry), so you should wait until one save has finished before calling another on that same document.

0reactions
nicholaswmincommented, Aug 24, 2016

Guess that’s another way to hit it - I’ll test both

Read more comments on GitHub >

github_iconTop Results From Across the Web

Eloquent push() and save() difference - laravel - Stack Overflow
It just shows that push() will update all the models related to the model in question, so if you change any of the...
Read more >
Query data in Azure Synapse Analytics - Azure Databricks
Learn how to read and write data to Azure Synapse Analytics using Azure Databricks.
Read more >
The best Spring Data JpaRepository - Vlad Mihalcea
Learn what is the best way to use the Spring Data JpaRepository so that you don't bump into the save method anti-pattern.
Read more >
Critical Issues Addressed in PAN-OS Releases
Bugs Affected Platform(if any). /Affected Version Description (release note) PAN‑92564 8.0.0‑8.0‑8, 8.1.0 PAN‑86882 8.0.0‑8.0.7. and all older Mainlines PAN‑81990 PA‑5220,PA‑5250. /. 8.0.4 Multiple DP restarts by...
Read more >
Debugging Memory Leaks in Node.js Applications - Toptal
Now let's record another Heap Allocations Snapshot and see which closures are occupying the memory. It's clear that SomeKindOfClojure() is our villain. Now...
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