Skip to content

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

Closed
@OIRNOIR

Description

@OIRNOIR

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledgedMongoose devs are aware of the issue and are working on a solution

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions