Skip to content

[BUG] Array as second parameter in the Populate method works awkwardly for ['-_id'] but works properly if the second parameter is string '-_id' #14231

@MHRSRoni

Description

@MHRSRoni

Prerequisites

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

Mongoose version

8.0.3

Node.js version

21.1.0

MongoDB server version

6.0.5

Typescript version (if applicable)

No response

Description

Array in the populate method as the second parameter behaves awkwardly for '-_id'. It replaces the path with the array of the second parameter, while it works if the second parameter is the string '-_id'.

Steps to Reproduce

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/populate').then(async connection => {
console.clear()
console.log("database connected successfully...")
console.log( "database name : ", connection.connections[0].name)
console.log()




//========= Schema ==========//
    const authorSchema = new mongoose.Schema({
        _id : Number,
        name : String,
        age : String
    }, {versionKey : false})

    const bookSchema = new mongoose.Schema({
        _id : Number,
        name : String,
        author : [
            {
                type : Number,
                ref : "author"
            }
        ]
    }, {versionKey : false})

    const Book = mongoose.model("book", bookSchema)

    const Author = mongoose.model("author", authorSchema)

//========= Document ==========//

    const authorData = [
        {_id : "1" , name : "summon",age : "29"},
        {_id : "2" , name : 'samad', age : '30'},
        {_id : "3" , name : 'minnar', age : '31'},
        {_id : "4", name : "rafat",age : "22"},
        {_id : "11", name : "roni",age : "23"},
    ]

    const bookData = [
        {_id : "1", name : "frontend developer", author : "1"},
        {_id : "2", name : "marketing ", author : "2"},
        {_id : "3", name : "teaching ", author : "3"},
        {_id : "4", name : "backend developer", author : "4"},
    ]

    await Author.insertMany(authorData)
    await Book.insertMany(bookData)

    const book = await Book.findById(1).populate("author", '-_id') //works

    const sameBook = await Book.findById(1).populate("author", ['-_id'])    //not works BUG

    console.log(book)
    /*output  for book :
    {
      _id: 1,
      name: 'frontend developer',
      author: [ { name: 'summon', age: '29' } ]
    }
    */
    console.log(sameBook)
    /*output for sameBook : 
    { _id: 1, name: 'frontend developer', author: [ { '0': '-_id' } ] }
    */
    

    console.log(".....completed")
})
.catch(err => console.log(err))

Expected Behavior

should throw an Error or remove the _id from the populated path

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions