-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.
Description
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.0
Node.js version
16.17.0
MongoDB server version
6.0.3
Typescript version (if applicable)
5.1.6
Description
Creating a schema with option
toObject: {
flattenObjectIds: true
}
has unwanted side effects.
MongoDB Compass shows the autogenerated _id field has type string, and Model.findById() returns null.
A workaround is to use toJSON() instead of toObject().
Steps to Reproduce
import { Model, Schema, model } from "mongoose";
interface IUser {
name: string;
}
const UserSchema = new Schema<IUser>(
{
name: String
},
{
toObject: { flattenObjectIds: true }
}
);
const User: Model<IUser> = model("User", UserSchema);
User.create({ name: "George" }).then((d) => {
User.findById(d._id)
.exec()
.then((d) => console.log(d));
});
Expected Behavior
Result is not null.
Metadata
Metadata
Assignees
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.