-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
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
TypeScript: Document type not passed along with collection() #11131
Comments
Not happening to me with the following code and same version. ...
const MyModel = model<IMyModel>('MyModel', myModelSchema);
await MyModel.updateOne({ _id: 'abc-ID'}, {
$set: { name: 'New team name' }
}); Btw try to update to 6.1.3 or change your code to something like this: await Teams.updateOne({ _id: 'abc-ID' }, { 'name': 'New team name' });
// where Teams is the model as MyModel above more info https://mongoosejs.com/docs/typescript/schemas.html |
True, but that solution completely avoids the wrongly defined types on the connection 😁 Another workaround would be this code (note the import mongoose from 'mongoose';
interface Team {
_id: string;
name: string;
}
await mongoose.connection.db.collection<Team>('teams').updateOne({_id : 'abc-ID'}, {
$set : {
'name' : 'New team name'
}
}); This GitHub issue is not meant to get a working solution - I got that. It's a contribution to help fix the types. The use case for the code posted above is data migrations. Models might have been updated to have default values or field validations that should not be used within the migration. |
I can reproduce with v6.1.3 and the error showed is:
it seems like the error is coming from mongodb, not mongoose types, if I'm not mistaken? |
Yes, the error is caused by the MongoDB types - and it should be that way. The underlying issue lies within the Mongoose types, however. Mongoose uses MongoDB's types for |
Do you want to request a feature or report a bug?
Bug 🐞
What is the current behavior?
This is a type definition issue.
Let's assume the documents in a collection have an
_id
of typestring
. When updating a document in that collection using a query for_id
, TypeScript shows the error "TS2322: Type 'string' is not assignable to type 'Condition'."If the current behavior is a bug, please provide the steps to reproduce.
Write this code:
Currently, MongoDB's types have a Document parameter while Mongoose's types do not:
MongoDB type (extracts)
Mongoose type (extract)
Mongoose uses MongoDB's type definition for updateOne. Since no parameter is passed to the Collection above, the default type "Document" is assumed and that requires an ObjectID.
My tsconfig.json:
What is the expected behavior?
The type of the function
mongoose.connection.collection
should have a parameterDocument
that is passed to the underlying MongoDB typeCollection
. Example:Then, the _id would be recognized as a string from the Document definition type parameter
TSchema
. No error would be shown.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: v16.13.1
Mongoose: 6.1.2
MongoDB: 4.4.10
The text was updated successfully, but these errors were encountered: