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

lean transform doesn't work & not return doccument #12143

Closed
2 tasks done
Hagriel opened this issue Jul 22, 2022 · 2 comments · Fixed by #12193
Closed
2 tasks done

lean transform doesn't work & not return doccument #12143

Hagriel opened this issue Jul 22, 2022 · 2 comments · Fixed by #12193
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Hagriel
Copy link

Hagriel commented Jul 22, 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.4.6

Node.js version

18.5

MongoDB server version

5.0.9

Description

the problem described in #12093 is not fixed

and in version 6.4.6 lean on the second level with transform not return any result

Steps to Reproduce

this.favoriteListModel
      .find({ userId })
      .populate({
        path: 'user',
        select: 'fullName avatar',
        transform: transform: (document: mongoose.Document, rec: mongoose.Document): mongoose.Document => {
                 delete document?._id;
                 delete document?.__v;

                 return document;
          },
      })
      .lean({
           virtuals: ['id', 'dataType'],
           transform: (document: mongoose.Document, rec: mongoose.Document): mongoose.Document => {
                 delete document?._id;
                 delete document?.__v;

                 return document;
          },
     });

Expected Behavior

Expected

{
    "id": "62d5d505e570e2fb20105b04",
    "title": "test list 1",
    "description": "test description",
    "user": {
      "fullName": "alex zed"
    },
    "dataType": "list"
  },

Now in v6.4.6 I get

{
    "_id": "62d5d505e570e2fb20105b04",        /// must not be
    "id": "62d5d505e570e2fb20105b04",
    "title": "test list 1",
    "description": "test description",
    "user": null,                             /// No data
    "dataType": "list"
  },
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 22, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String,
    item: {
        type: mongoose.Types.ObjectId,
        ref: 'Reference'
    }
});

const referencedSchema = new mongoose.Schema({
    title: String
});

const Test = mongoose.model('Test', testSchema);

const Reference = mongoose.model('Reference', referencedSchema);


async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    const reference = await Reference.create({
        title: 'The referenced document'
    });

    await Test.create({
        name: 'Test Testerson',
        item: reference._id
    });

    let test = await Test.findOne().populate('item');
    
    console.log(test);

    test = await Test.findOne().populate('item').lean({transform: (doc) => {
        delete doc._id;
        delete doc.__v;
        return doc;
    }});

    console.log(test);
}

run();

@vkarpov15 vkarpov15 added this to the 6.4.8 milestone Jul 25, 2022
@vkarpov15
Copy link
Collaborator

The issue is more due to deleting _id, which is making it hard for populate to map the populate result document to the top level document.

vkarpov15 added a commit that referenced this issue Aug 3, 2022
fix(model+query): handle populate with lean transform that deletes `_id`
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