-
-
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
includeResultMetadata types not changing return type? #14303
Comments
Where in the docs do you see that Also, is the error just in your IDE or also in TypeScript? Because I'm unable to repro with TypeScript, the following script compiles fine: import mongoose from 'mongoose';
run();
async function run() {
const TestModel = mongoose.model('Test', new mongoose.Schema({ name: String }));
const doc = await TestModel.findOneAndUpdate({}, { name: 'bar' }, { includeResultMetadata: true });
doc.value;
}
|
see here:
and here https://www.mongodb.com/blog/post/behavioral-changes-find-one-family-apis-node-js-driver-6-0-0
I will look into it again late,r but it was a build issue, not just an IDE issue. But some other issues are even valid, I had returnDocument: 'after' in some updateOne queries, which makes no sense of course ;) |
Regarding includeResultMetadata,
so this should be safe, it wsa just kinda of confusing ;-) regarding the typescript issue, I iwll look into this now |
Alright, I found the issue. It's about the "lean" flag. import mongoose from 'mongoose';
run();
async function run() {
const TestModel = mongoose.model('Test', new mongoose.Schema({ name: String }));
const doc = await TestModel.findOneAndUpdate({}, { name: 'bar' }, { includeResultMetadata: true, lean: true, returnDocument: 'after' });
doc.value;
} the problem pops up when I add This brings me to another followup question: If we set includeResultMetadata to true, is mongoose then populating stuff and returns a mongoose document, or does it actually return the raw result like with lean: true? |
…esultMetadata` and `lean` set Fix #14303
This brings me to another followup question: If we set includeResultMetadata to true, is mongoose then populating stuff and returns a mongoose document, or does it actually return the raw result like with lean: true? If I would expect it to be the lean version, as this was the behaviour with "rawResult" before. But to be explicit about that, what happens then if I set lean explicity to false? Setting |
types(model): correct return type for findOneAndUpdate with `includeResultMetadata` and `lean` set
Prerequisites
Mongoose version
8.1.1
Node.js version
20.x
MongoDB server version
6.x
Typescript version (if applicable)
5.1.6
Description
I tried upgrading to mongoose 8.x and changed rawResult: true to includeResultMetadata: true, but now typescript complains:
also it seems that the retutrn type is not changed depending on the value of includeResultMetadata
it's the same as in the screenshot, regardless of this flag.
Furthermore; i found it kinda confusing with the mongodb docs, as they say that includeResultMetadata is true by default. Does mongoose set it to false then by default? Or is it actually true, but mongoose just returns the document and strips the rest away? Which would sound to me of a waste of some extra bytes?
error TS2339: Property 'value' does not exist on type 'FlattenMaps & Required<{ _id: ObjectId; }>'.
error TS2339: Property 'lastErrorObject' does not exist on type 'FlattenMaps & Required<{ _id: ObjectId; }>'.
error TS2339: Property 'value' does not exist on type 'FlattenMaps & Required<{ _id: ObjectId; }>'.
Steps to Reproduce
use an example query for findOneAndupdate and play around with the includeResultMetadata flag
Expected Behavior
should return the "ModifyResult" instead, like it did with rawResult: true
The text was updated successfully, but these errors were encountered: