Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document $inc method does not store previous data #13274

Closed
2 tasks done
oleegarch opened this issue Apr 13, 2023 · 2 comments
Closed
2 tasks done

Document $inc method does not store previous data #13274

oleegarch opened this issue Apr 13, 2023 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@oleegarch
Copy link

oleegarch commented Apr 13, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.7.2

Node.js version

v16.0.0

MongoDB server version

4.4.5

Typescript version (if applicable)

No response

Description

$inc method locally saves all data, but sends to mongoose the last one

const user = await Users.findOne({ snid: 'vk_205580268' });

user.$inc('coins', 1000); // user.coins = 1000
user.$inc('coins', 2000); // user.coins = 3000

await user.save();

// but output:
// Mongoose: users.updateOne({ _id: new ObjectId("640e547132b7729ceb60d6d6") }, { '$inc': { coins: 2000 } }, { session: null })

Steps to Reproduce

Execute $inc method twice or more times

Expected Behavior

$inc method store previous value and save all together, output should be:
Mongoose: users.updateOne({ _id: new ObjectId("640e547132b7729ceb60d6d6") }, { '$inc': { coins: 3000 } }, { session: null })

@floatingmess06
Copy link

does:
user.$inc('coins', 1000); // user.coins = 1000
await user.save();
user.$inc('coins', 2000); // user.coins = 3000
await user.save();

  • works or not

@vkarpov15 vkarpov15 added this to the 6.10.6 milestone Apr 19, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Apr 19, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Apr 19, 2023
@IslandRhythms
Copy link
Collaborator

I am able to reproduce on 7.0.4, but as floatingmess06 pointed out if you do a save operation beforehand it works fine.

const mongoose = require('mongoose');
mongoose.set('debug', true);
const testSchema = new mongoose.Schema({
  coins: Number
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({ coins: 0 });

  const doc = await Test.findOne();
  doc.$inc('coins', 1000);
  console.log('what is doc', doc);
  doc.$inc('coins', 2000);
  console.log('what is doc', doc);

  await doc.save();
  console.log('doc after save', doc);
  console.log(await Test.findOne());
  console.log('done');
}

run();

vkarpov15 added a commit that referenced this issue Apr 25, 2023
Merge Document `$inc` calls instead of overwriting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants