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

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

Closed
2 tasks done
MHRSRoni opened this issue Jan 3, 2024 · 2 comments · Fixed by #14242
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@MHRSRoni
Copy link

MHRSRoni commented Jan 3, 2024

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

@MHRSRoni MHRSRoni changed the title Array as second parameter in the Populate method works awkwardly for ['-_id'] but works properly if the second parameter is string '-_id' [BUG] Array as second parameter in the Populate method works awkwardly for ['-_id'] but works properly if the second parameter is string '-_id' Jan 3, 2024
@vkarpov15 vkarpov15 added this to the 8.0.5 milestone Jan 4, 2024
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue 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 Jan 4, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.0.5, 8.0.4 Jan 8, 2024
vkarpov15 added a commit that referenced this issue Jan 9, 2024
fix(populate): handle deselecting `_id` with array of fields in `populate()`
@MHRSRoni
Copy link
Author

thank you

@MHRSRoni
Copy link
Author

but though I have created the issue, I was not counted as a contributor. It's quite sad. 😐

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.

2 participants