Skip to content

_id appended even though _id is set to false in the schema #8274

Closed
@sebastian-nowak

Description

@sebastian-nowak

Run the following code in mongoose 5.7.5:

#!/usr/bin/env node
(async function () {
  const mongoose = require('mongoose');

  mongoose.plugin((schema) => {
    schema.options.versionKey = false;
    schema.options.minimize = false;
    schema.options.strict = 'throw';
    schema.options.strictQuery = 'throw';
  });

  mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});


  const schema = new mongoose.Schema({
    _id: {
      type: mongoose.Schema.Types.ObjectId,
      default: () => mongoose.Types.ObjectId(),
      required: true
    },

    operations: {
      type: [new mongoose.Schema({
        _id: {
          type: mongoose.Schema.Types.ObjectId,
          default: () => mongoose.Types.ObjectId(),
          required: true
        },

        action: {
          type: String,
          required: true
        }
      }, {
        discriminatorKey: 'action'
      })],
      default: [],
      required: true
    }
  });

  schema.path('operations').discriminator(
    `asd/do-something`,
    new mongoose.Schema({
      wall: {
        type: mongoose.Schema.Types.ObjectId,
        required: true
      },

      pitchPath: {
        type: new mongoose.Schema({
          _id: {
            type: mongoose.Schema.Types.ObjectId,
            required: true
          },
          pitch: {
            type: mongoose.Schema.Types.ObjectId,
            required: true
          },
          path: {
            type: [{
              _id: false,
              x: {
                type: Number,
                required: true
              },
              y: {
                type: Number,
                required: true
              },
              visible: {
                type: Boolean,
                default: true,
                required: true
              }
            }],
            default: undefined,
            required: true
          }
        }),
        default: undefined,
        required: true
      }
    })
  );

  const Model = mongoose.model('test', schema);
  const instance = new Model();
  instance.operations.push({
    action: 'asd/do-something',
    wall: mongoose.Types.ObjectId(),
    pitchPath: {
      _id: mongoose.Types.ObjectId(),
      pitch: mongoose.Types.ObjectId(),
      path: [
        {x: 0, y: 0, visible: true},
        {x: 1, y: 1, visible: true}
      ]
    }
  });

  console.log(instance.operations[0].pitchPath);
})();

This is the output:

{
  _id: 5db02ad12f42a48fa5cc1d31,
  pitch: 5db02ad12f42a48fa5cc1d32,
  path: [
    { visible: true, _id: 5db02ad12f42a48fa5cc1d34, x: 0, y: 0 },
    { visible: true, _id: 5db02ad12f42a48fa5cc1d33, x: 1, y: 1 }
  ]
}

Notice that entries in the path array have ids assigned, but there's _id: false in the schema.

Activity

added this to the 5.7.8 milestone on Oct 28, 2019
added
has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue
on Oct 28, 2019
added a commit that references this issue on Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vkarpov15@sebastian-nowak

        Issue actions

          _id appended even though _id is set to false in the schema · Issue #8274 · Automattic/mongoose