Skip to content
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

Select of a Map type with select:false not working as expected #12603

Closed
2 tasks done
jlvcm opened this issue Oct 27, 2022 · 0 comments · Fixed by #12616
Closed
2 tasks done

Select of a Map type with select:false not working as expected #12603

jlvcm opened this issue Oct 27, 2022 · 0 comments · Fixed by #12616
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@jlvcm
Copy link

jlvcm commented Oct 27, 2022

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.2

Node.js version

16.16.0

MongoDB server version

5.0.3

Typescript version (if applicable)

No response

Description

After version 6.6.2 the select with a + on a field of type Map with select: false does not seem to work as expected, it just does not return the element, i'm not sure but it may be related with this change:

  • fix(query): handle select: false on map paths in query results #12467

Steps to Reproduce

const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');

async function main(){
  // Connect to a mongodb server
  await mongoose.connect((await MongoMemoryServer.create({ binary: { version: '5.0.3' } })).getUri());

  // Create the model
  const MyModel = mongoose.model('ModelName', new mongoose.Schema({
    title: String,
   // Create the type we are testing, a Map with `select:false` 
   body: {
      type: Map,
      of: { en: String, pt: String },
      select: false,
    }
  }));
  // Set the body
  await MyModel.create({
    title: "teste",
    body: {
      'A':{'en': 'en test A value', 'pt': 'pt test A value'},
      'B':{'en': 'en test B value', 'pt': 'pt test B value'}
    }
  })
  // Get the all items with the body field.
  console.log(await MyModel.find({}).select('+body').lean());
}

main();

Expected Behavior

We expect the body to be returned:

[
  {
    "_id": new ObjectId("635a471c7cf95eff198f4b2f"),
    title: 'teste',
    "body": {
      "A": {
        "en": "en test A value",
        "pt": "pt test A value"
      },
      "B": {
        "en": "en test B value",
        "pt": "pt test B value"
      }
    }
    __v: 0
  }
]

But on version >=6.6.2 it just returns the title:

[
  {
    _id: new ObjectId("635a471c7cf95eff198f4b2f"),
    title: 'teste',
    __v: 0
  }
]

It seems that the issue requires a type Map, a select with a + and mongoose version >= 6.6.2
we can get the body by changing:

  • mongoose to version 6.6.1 or lower
  • use type: Object, instead of Map
  • remove the + from the select (but not what we want)
@hasezoey hasezoey added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Oct 31, 2022
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Oct 31, 2022
@vkarpov15 vkarpov15 added this to the 6.7.1 milestone Oct 31, 2022
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants