mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

populate not working without lean()

Open Arjun059 opened this issue 1 year ago • 3 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.x.x

Node.js version

18.x

MongoDB server version

7.x.x

Typescript version (if applicable)

No response

Description

populate not working without lean(). let query = Model.find({}, {}); query.populate({path: "path", select: "select fields" }) // not working let data = await query; // not working

query.populate({path: "path", select: "select fields" }).lean() // works fine let data = await query; // work fine

Steps to Reproduce

populate not working without lean(). let query = Model.find({}, {}); query.populate({path: "path", select: "select fields" }) // not working let data = await query; // not working

query.populate({path: "path", select: "select fields" }).lean() // works fine let data = await query; // work fine

Expected Behavior

populate should work without lean()

Arjun059 avatar Jul 23 '24 06:07 Arjun059

Please clarify what you mean by "not working" - are you getting some sort of error message?

vkarpov15 avatar Jul 23 '24 17:07 vkarpov15

Please clarify what you mean by "not working" - are you getting some sort of error message?

There is no error, but the populated document is not returned when I use Model.populate({ path: "pathField", select: "apple mango"}). However, it works fine with the string Model.populate("pathField").

Arjun059 avatar Jul 24 '24 05:07 Arjun059

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Aug 08 '24 00:08 github-actions[bot]

I'm unable to repro, the following script shows same result with and without lean()

const mongoose = require('mongoose');

const AuthorSchema = new mongoose.Schema({
    name: String,
});
const BookSchema = new mongoose.Schema({
    title: String,
    author: { type: mongoose.Schema.Types.ObjectId, ref: 'Author' }
});
// Create models
const Author = mongoose.model('Author', AuthorSchema);
const Book = mongoose.model('Book', BookSchema);
async function main() {
    // Connect to MongoDB
    await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

    // Clean up the database
    await Author.deleteMany({});
    await Book.deleteMany({});

    // Create test data
    const author = await Author.create({ name: 'John Doe' });
    await Book.create({ title: 'Mongoose Guide', author: author._id });

    // Test populate without lean()
    let query = Book.find({});
    query.populate({ path: 'author', select: 'name' });
    let data = await query;
    console.log('Without lean():', data);

    // Test populate with lean()
    query = Book.find({});
    query.populate({ path: 'author', select: 'name' }).lean();
    data = await query;
    console.log('With lean():', data);

    // Close the connection
    await mongoose.connection.close();
}

main().catch(err => console.error(err));

Output:

$ node gh-14757.js 
Without lean(): [
  {
    _id: new ObjectId('66ba5409c961677682494970'),
    title: 'Mongoose Guide',
    author: { _id: new ObjectId('66ba5409c96167768249496e'), name: 'John Doe' },
    __v: 0
  }
]
With lean(): [
  {
    _id: new ObjectId('66ba5409c961677682494970'),
    title: 'Mongoose Guide',
    author: { _id: new ObjectId('66ba5409c96167768249496e'), name: 'John Doe' },
    __v: 0
  }
]

Here is Mongoose playground link

vkarpov15 avatar Aug 12 '24 18:08 vkarpov15

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Aug 27 '24 00:08 github-actions[bot]

This issue was closed because it has been inactive for 19 days and has been marked as stale.

github-actions[bot] avatar Sep 01 '24 00:09 github-actions[bot]