push() and save() leaks it's query on the next save() as well
See original GitHub issueOverview
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 simplypush
-es an item into an array- I then
save()
- I then
- The
switchBoard
case, which simply toggles a value, that lies outside of the array- I then
save()
- I then
I’m expecting to see mongoose sending 2 distinct queries:
- A
push
query to push the item into thethis.boardBucket.items
Array - A
set
query to set the valueboardBucket.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:
- Created 7 years ago
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Guess that’s another way to hit it - I’ll test both