Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.95k stars 3.84k forks source link

Subdocs in array no longer have default _id #12438

Closed dragos99 closed 2 years ago

dragos99 commented 2 years ago

Prerequisites

Mongoose version

6.6.1

Node.js version

16.15.0

MongoDB server version

5.0.12

Description

Subdocuments added in array no longer have an _id field.

Steps to Reproduce

Example:

const schema = new Schema({
    children: [{ name: String }]
});

Object in children have no _id field.

Expected Behavior

No response

hasezoey commented 2 years ago

is this a issue for runtime or for typescript types?

also, from what i can tell at runtime, it still creates _id on subdocuments unless _id: false is set

Code Used:

// NodeJS: 18.8.0
// MongoDB: 5.0 (Docker)
// Typescript 4.8.3
import * as mongoose from 'mongoose'; // mongoose@6.6.1

const userSchema = new mongoose.Schema({
  children: [
    {
      name: String,
    },
  ],
});

const UserModel = mongoose.model('User', userSchema);

(async () => {
  await mongoose.connect(`mongodb://localhost:27017/`, {
    dbName: 'verifyMASTER',
  });

  const doc = new UserModel(); // default nothing added
  console.log('inital doc', doc);

  doc.children.push({ name: 'hello' });
  console.log('doc after push', doc);

  await doc.save();
  console.log('doc after save', doc);

  await mongoose.disconnect();
})();

output:

inital doc { _id: new ObjectId("6327166a0870d5f072b8c307"), children: [] }
doc after push {
  _id: new ObjectId("6327166a0870d5f072b8c307"),
  children: [ { name: 'hello', _id: new ObjectId("6327166a0870d5f072b8c308") } ]
}
doc after save {
  _id: new ObjectId("6327166a0870d5f072b8c307"),
  children: [ { name: 'hello', _id: new ObjectId("6327166a0870d5f072b8c308") } ],
  __v: 0
}

Reproduction Repository / Branch: https://github.com/typegoose/typegoose-testing/tree/mongooseGh12438

Please edit the code to reproduce your issue or extend your description of the problem

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 2 years ago

This issue was closed because it has been inactive for 19 days since being marked as stale.