Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.88k stars 3.83k forks source link

populate not working without lean() #14757

Closed Arjun059 closed 2 weeks ago

Arjun059 commented 1 month ago

Prerequisites

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()

vkarpov15 commented 1 month ago

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

Arjun059 commented 1 month ago

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").

github-actions[bot] commented 1 month ago

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

vkarpov15 commented 1 month ago

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

github-actions[bot] commented 2 weeks ago

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] commented 2 weeks ago

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