We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
7.4.0
16.17.0
6.0.3
5.1.6
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.
_id
string
Model.findById()
null
A workaround is to use toJSON() instead of toObject().
toJSON()
toObject()
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)); });
Result is not null.
The text was updated successfully, but these errors were encountered:
import { Model, Schema, model, connect, connection } from "mongoose"; interface IUser { name: string; } const UserSchema = new Schema<IUser>( { name: String }, { toObject: { flattenObjectIds: true } } ); const User: Model<IUser> = model("User", UserSchema); async function run() { await connect('mongodb://localhost:27017'); await connection.dropDatabase(); User.create({ name: "George" }).then((d) => { User.findById(d._id) .exec() .then((d) => console.log(d)); }); } run();
Sorry, something went wrong.
flattenObjectIds
Merge pull request #13658 from Automattic/IslandRhythms/gh-13648
255493b
fix: setting `flattenObjectIds` returns null doc
Successfully merging a pull request may close this issue.
Prerequisites
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
has unwanted side effects.
MongoDB Compass shows the autogenerated
_id
field has typestring
, andModel.findById()
returnsnull
.A workaround is to use
toJSON()
instead oftoObject()
.Steps to Reproduce
Expected Behavior
Result is not
null
.The text was updated successfully, but these errors were encountered: