Comments (5)
Where in the docs do you see that includeResultMetadata
is true by default? I think that changed in MongoDB Node driver 6.
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;
}
$ ./node_modules/.bin/tsc --strict gh-14303.ts
$
from mongoose.
Where in the docs do you see that
includeResultMetadata
is true by default? I think that changed in MongoDB Node driver 6.
see here:
https://github.com/mongodb/node-mongodb-native/releases/tag/v5.7.0
This option defaults to true, which will return a ModifyResult type. When set to false, which will
and here https://www.mongodb.com/blog/post/behavioral-changes-find-one-family-apis-node-js-driver-6-0-0
Starting with the Node.js Driver 5.7.0 release a new FindOneAndOptions property called includeResultMetadata has been introduced. When this property is set to false (default is true) the findOneAnd APIs will return the requested document as expected.
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:
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 ;)
from mongoose.
Regarding includeResultMetadata,
you are right, it's mentionted here that it will be set to false. https://github.com/mongodb/node-mongodb-native/releases/tag/v6.0.0
Once 6.0.0 is released, includeResultMetadata: false will become the default behavior. If your application relies on the previous behavior of these APIs, setting includeResultMetadata: true will allow you to continue to access the ModifyResult directly.
so this should be safe, it wsa just kinda of confusing ;-)
regarding the typescript issue, I iwll look into this now
from mongoose.
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 lean: true
to the options. then the returned type definitoin is the one I mentioned.
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?
Because the type of includeResultMetadta: false is the exact same as the ".value" prop when I set it to true...
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?
from mongoose.
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 lean
is false, then Mongoose will fully hydrate the document, and .value
will contain the fully hydrated document.
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 lean: false
is the same as not setting lean
at all in this case.
from mongoose.
Related Issues (20)
- `InferSchemaType` returns `unknown` value HOT 4
- not getting validation error even i am using validate method in my schema HOT 3
- Mongoose v6 modifies connection options object HOT 4
- Model.insertMany with `lean: true` is very slow compared to native driver
- fix: Cannot run `mongoose.createConnection` under `tsx`/ `esbuild`/ `bun` due to the `nextConnectionId` HOT 4
- Populate with TypeScript not working on Cursor HOT 1
- MongoNetworkTimeoutError crashes nodejs process HOT 4
- Missing Typescript detail on Mongoose-specific `options` for updateOne, updateMany, etc. HOT 3
- Facing issue in Mongo+srv which i passing through github actions secrets
- Convenience Method for Converting Array of Strings to ObjectIDs HOT 3
- Round-tripped document fails validation on unmodified Date field HOT 1
- Docs for overwrite option for mongoose.model(), and overwriteModels for mongoose global
- New Mongoose error suddenly due to MongooseError: Buffering timeout HOT 19
- Improve Documentation HOT 3
- BulkSave on entities with virtual properties *and* a transaction: massive performance hit and OOM crashes HOT 6
- Filter query `Condition` type might as well be `any` HOT 3
- pre middleware .save conflict with strict mode HOT 7
- Date coercion and typechecking inconsistent on `bulkWrite` HOT 3
- Bug while creating document HOT 4
- `validateAllPaths` option to `validate()` and `save()` that validates all paths in the schema, not just modified paths HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.