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

Minimize does sometimes not working. Empty object is saved. #13782

Closed
2 tasks done
m0rtalis opened this issue Aug 25, 2023 · 1 comment
Closed
2 tasks done

Minimize does sometimes not working. Empty object is saved. #13782

m0rtalis opened this issue Aug 25, 2023 · 1 comment
Labels
backwards-breaking enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@m0rtalis
Copy link

Prerequisites

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

Mongoose version

7.4.4

Node.js version

16.20.2

MongoDB server version

7.0.0

Typescript version (if applicable)

No response

Description

Empty objects are sometimes deleted, sometimes not.

Steps to Reproduce

  mongoose.connect("mongodb://127.0.0.1:27017/test");
  const schema = new Schema({
    metadata: {
      type: {},
      default: {},
      required: true,
      _id: false
    }
  }, { minimize: true });
  const Model = model("Schema", schema);
  const m = new Model({ metadata: {} });
  await m.save();
  console.log("Document has no metadata property")

  const x = await Model.findById(m._id).exec();
  x.metadata = {};
  await x.save();
  console.log(x.$isEmpty('metadata')) // true
  console.log("Document now has empty metadata property");

Expected Behavior

The saved document has the form

{
  "_id": {
    "$oid": "64e87a78972e351e7096f778"
  },
  "__v": 0
}

but it is

{
  "_id": {
    "$oid": "64e87a78972e351e7096f773"
  },
  "__v": 0,
  "metadata": {}
}
@vkarpov15 vkarpov15 added this to the 7.5.1 milestone Aug 25, 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 Aug 25, 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 Aug 29, 2023
@IslandRhythms
Copy link
Collaborator

Checked in compass and an empty object is there

const mongoose = require('mongoose');


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

  const schema = new mongoose.Schema({
    metadata: {
      type: {},
      default: {},
      required: true,
      _id: false
    }
  }, { minimize: true });
  const Model = mongoose.model("Schema", schema);
  const m = new Model({ metadata: {} });
  await m.save();
  console.log("Document has no metadata property")

  const x = await Model.findById(m._id).exec();
  x.metadata = {};
  await x.save();
  console.log(x.$isEmpty('metadata')) // true
  console.log("Document now has empty metadata property");
  console.log('what is x', x);
}

run();

@vkarpov15 vkarpov15 modified the milestones: 7.5.1, 7.5.2, 8.0 Sep 11, 2023
vkarpov15 added a commit that referenced this issue Sep 26, 2023
BREAKING CHANGE: apply minimize by default when updating document
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature backwards-breaking and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Sep 26, 2023
vkarpov15 added a commit that referenced this issue Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards-breaking enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

3 participants