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

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

Closed
sebastian-nowak opened this issue Oct 23, 2019 · 0 comments
Closed

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

sebastian-nowak opened this issue Oct 23, 2019 · 0 comments
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@sebastian-nowak
Copy link

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.

@vkarpov15 vkarpov15 added this to the 5.7.8 milestone Oct 28, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Oct 28, 2019
vkarpov15 added a commit that referenced this issue Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants