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
6.6.5
Node.js version
14.19.3
MongoDB server version
4.4
Description
I'm trying to upgrade to v6 of mongoose but keep receiving multiple errors related to ObjectId
Types of property 'evidence' are incompatible.
Type 'LeanDocument<ObjectId>[] | undefined' is not assignable to type 'ObjectId[] | undefined'.
Type 'LeanDocument<ObjectId>[]' is not assignable to type 'ObjectId[]'.
Type 'LeanDocument<ObjectId>' is missing the following properties from type 'ObjectId': toJSON, equals
That was new a error showing up after upgrade in a lot of places, LeanDocument 🤔 ?
I was expecting it to be original ObjectId, but I'm not sure if there is any way to change that other than updating interface?
Then that lead me to reading up recommend logic for ObjectId at this page: ObjectIds and Other Mongoose Types
Suggested ObjectId types
To define a property of type ObjectId
, you should use Types.ObjectId
in the TypeScript document interface. You should use ObjectId
or Schema.Types.ObjectId
in your schema definition.
Current codebase setup
Current approach is to use bson ObjectId as interface type and Schema.Types.ObjectId
as schema type, therefore this will require to update all interfaces to use Types.ObjectId
instead of bson ObjectId.
Now this is where the real problem is, seems like Types.ObjectId is not actually compatible with bson ObjectId due to required _id
property. That would require huge amount of changes to replace new ObjectId()
with new Types.ObjectId()
. Is this an overlook? What is the reason Types.ObjectId is not compatible the actual underlying ObjectId class?
// Property '_id' is missing in type ('bson").ObjectID' but required in type 'import("mongoose").Types.ObjectId'.
`node_modules/bson/bson").ObjectID' is not assignable to parameter of type 'import("mongoose").Types.ObjectId'`
Next steps
Determine if this an issue with queries now returning LeanDocument<ObjectId>
instead of ObjectId
? or incompatibility between Types.ObjectId
and bson ObjectId
?
I'm surprised this have not yet come up more 🤔
Let me know what is the best way to processed with upgrade!
Steps to Reproduce
import { Schema, Types } from 'mongoose';
import { ObjectId } from 'bson';
// 1. Create an interface representing a document in MongoDB.
interface IUser {
name: string;
email: string;
organizationId: ObjectId;
}
// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true },
// And `Schema.Types.ObjectId` in the schema definition.
organizationId: { type: Schema.Types.ObjectId, ref: 'Organization' }
});
const User = model<IUser>('User', userSchema);
const user = new User()
// Property '_id' is missing in type ('bson").ObjectID' but required in type 'import("mongoose").Types.ObjectId'.
user.organizationId = new ObjectId()
Expected Behavior
bson
ObjectId should be accepted as Types.ObjectId interface or query should not return LeanDocument<ObjectId>