Skip to content

[Bug] Document.prototype.populate() options.retainNullValues not working. #8293

@bongjinpark1

Description

@bongjinpark1

nodejs version : 10.16.3
mongooser version : 5.7.7

teams.js

const mongoose = require('mongoose')
const fields = {
  troops: [{ type: Number, ref: 'Card' }]
}

const options = {
  versionKey: false,
  timestamps: true
}

const schema = new mongoose.Schema(fields, options)

const Team = mongoose.model('Team', schema)

module.exports = Team

cards.js

const mongoose = require('mongoose')

const fields = {
  _id: { type: Number },
  name: { type: String, unique: true },
  entityType: { type: String }
}

const options = {
  discriminatorKey: 'entityType'
}

const schema = new mongoose.Schema(fields, options)

const Card = mongoose.model('Card', schema)

module.exports = Card

index.js

    const Team = require('path of teams.js')
    const team = new Team({
      troops: [1, 2, 3, 4]
    })

    await team.save()

    await team
      .populate([
        {
          path: 'troops',
          select: 'name',
          retainNullValues: true, // not working
          options: {
            retainNullValues: true // not working too.
          }
        }
      ])
      .execPopulate()

    console.log(team)

/*
expected:
{
troops: [null, {name: 'name of 2'}, {name: 'name of 3'}, {name: 'name of 4'}]
}

actual:
{
troops: [{name: 'name of 2'}, {name: 'name of 3'}, {name: 'name of 4'}]
}
*/

How can i get populated array include null value? any ideas?

thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions