Git Product home page Git Product logo

Comments (5)

vkarpov15 avatar vkarpov15 commented on September 21, 2024

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.

simllll avatar simllll commented on September 21, 2024

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.

simllll avatar simllll commented on September 21, 2024

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.

image

so this should be safe, it wsa just kinda of confusing ;-)

regarding the typescript issue, I iwll look into this now

from mongoose.

simllll avatar simllll commented on September 21, 2024

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.

vkarpov15 avatar vkarpov15 commented on September 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.