Skip to content

findOneAndUpdate with upsert option won't respect "_id: String" #12128

@eric-burel

Description

@eric-burel

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

Node.js version

16.15.0

MongoDB server version

4.0.4

Description

The inserted document will have an "ObjectId" _id despite using _id: String in a schema.

new mongoose.Schema({ _id: String }, { strict: false })

Steps to Reproduce

  • Create a new model with _id: String in the schema
const schema = new Schema({ _id: String }, { strict: false});
const MyModel = mongoose.model('MyModel', schema);
  • Use findOneAndUpdate with upsert option to create a new document:
// since the doc doesn't exist, it will be inserted thanks to the upsert option
    const res =
      await MyModel.findOneAndUpdate(
        { foo: "bar" },
        {},
        { upsert: true, returnDocument: "after" }
      );

The document will have an ObjectId _id, the String type is not respected.

Expected Behavior

The new document should have a string _id.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stalecan't reproduceMongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions