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

id property of object is not written to the database when using model.replaceOne #13867

Closed
2 tasks done
OIRNOIR opened this issue Sep 16, 2023 · 5 comments
Closed
2 tasks done
Labels
acknowledged Mongoose devs are aware of the issue and are working on a solution
Milestone

Comments

@OIRNOIR
Copy link

OIRNOIR commented Sep 16, 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

7.5.2

Node.js version

v18.17.1

MongoDB server version

7.0.0

Typescript version (if applicable)

No response

Description

I believe this issue has been caused by this: #13762
In my program, I have a structure that has an id property. This id is my own ID that I assign it using a separate system than mongodb's _id. These properties are separate. They have always worked just fine as separate properties, until version 7.5.2.
Upon updating to version 7.5.2, I began to notice that, for some reason, ids were being set to undefined.
I did some investigating, and it turns out that it was the replaceOne function, which for some reason, since version 7.5.2, now completely ignores the id parameter.

Steps to Reproduce

Create a model with at least the below properties:

	id: {
		type: String,
		required: true,
		unique: true
	},
	someOtherProperty: String

Then,

// Create a document with an ID
await yourModel.create({id: "6969", "someOtherProperty": "FooBar"});
// Confirm that it is in the database by finding it by its ID
let document = await yourModel.findOne({id: "6969"});
console.log(document); // {id: "6969", "someOtherProperty": "FooBar"}

// Use the replaceone function on it in any way (Can be just setting it to itself)
await yourModel.replaceOne({id: document.id}, document);

// Check if it's still in the database by the ID
let doc2 = await yourModel.findOne({id: "6969"}); // null
// List all documents in the model
let all = await yourModel.find({}); // [ { "someOtherProperty": "FooBar" } ]
// The ID property has been stripped from the document.

Expected Behavior

Simply, the ID property gets treated as a property of the document when being used in replaceOne, and not discarded. The second to last step in the Steps to Reproduce would log the document as it is after having been modified by replaceOne (in this case, not modified at all, but copied).

This was referenced Sep 16, 2023
@IslandRhythms IslandRhythms added the acknowledged Mongoose devs are aware of the issue and are working on a solution label Sep 18, 2023
@VeniMitev
Copy link

This is a breaking change on a minor package update. Something like this can cause a lot of issues for applications, teams, and users. This needs to be addressed ASAP. It took us a while to figure out what the issue came from, but it created a lot of chaos with our applications and complaints from end users. Our app queries by the "id" property that was set by us and is different from the "_id" from Mongoose. Our user's data just doesn't show up because Mongoose disregards the "id" property when saving to the DB.

@frank-forma
Copy link

can also confirm this is a breaking change for our company, a lot of our integration tests are failing due as a result

@isnifer
Copy link

isnifer commented Sep 21, 2023

Same here — getting issues on insert. Validation for array field is talking to me that id field is required

@OIRNOIR
Copy link
Author

OIRNOIR commented Sep 21, 2023

I made this issue because I discovered that it was a breaking change for my projects. I have kept my mongoose held back to 7.5.1 for the meantime.

@vkarpov15 vkarpov15 added this to the 7.5.4 milestone Oct 1, 2023
@vkarpov15
Copy link
Collaborator

You're right, this is an unintended breaking change, we reverted in #13933 and will ship 7.5.4 shortly. We're very sorry about this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged Mongoose devs are aware of the issue and are working on a solution
Projects
None yet
Development

No branches or pull requests

6 participants