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 is missing in subDoc #12515

Closed
2 tasks done
abarriel opened this issue Oct 4, 2022 · 1 comment · Fixed by #12523
Closed
2 tasks done

_id is missing in subDoc #12515

abarriel opened this issue Oct 4, 2022 · 1 comment · Fixed by #12523
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@abarriel
Copy link
Contributor

abarriel commented Oct 4, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.3

Node.js version

16

MongoDB server version

4.9.1

Description

Hello, _id is missing from subDoc when saving. It shouldn't, _id should be set in array. You can run the script to see Animal Elephant is missing _id property.

Steps to Reproduce

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const animalSchema = new Schema(
  {
    title: String,
  },
  { _id: true },
);

const animalsSchema = new Schema({
  species: [animalSchema],
  totalAnimals: Number,
});

const Userschema = new Schema({
  animals: animalsSchema,
});

const UserModel = mongoose.model("UserAnimal", Userschema);

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

 // init one doc
  const doc = new UserModel();
  doc.animals = { totalAnimals: 1 };
  doc.animals.species = [{ title: "Lion" }];
  await doc.save();

  // once created we fetch it again
  const user = await UserModel.findById(doc._id);
  console.log({ user });
  
  // add new animal
  user.animals.species.push({ title: "Elephant" });
  await user.save();
  console.log("Elephant is missing _id !", user.animals.species);

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

Expected Behavior

No response

@hasezoey
Copy link
Collaborator

hasezoey commented Oct 4, 2022

can reproduce

Code used

Ignore the ts errors when using with typescript

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

const Schema = mongoose.Schema;

const animalSchema = new Schema(
  {
    title: String,
  },
  { _id: true }
);

const animalsSchema = new Schema({
  species: [animalSchema],
  totalAnimals: Number,
});

const Userschema = new Schema({
  animals: animalsSchema,
});

const UserModel = mongoose.model('UserAnimal', Userschema);

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

  // init one doc
  const doc = new UserModel();
  doc.animals = { totalAnimals: 1 };
  doc.animals.species = [{ title: 'Lion' }];
  await doc.save();

  // once created we fetch it again
  const user = await UserModel.findById(doc._id);
  console.log(user);

  // add new animal
  user.animals.species.push({ title: 'Elephant' });
  await user.save();
  console.log('Elephant is missing _id !', user.animals.species);

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

output:

{
  _id: new ObjectId("633c4d1f2441695e1b7dc3d3"),
  animals: {
    totalAnimals: 1,
    _id: new ObjectId("633c4d1f2441695e1b7dc3d4"),
    species: [ [Object] ]
  },
  __v: 0
}
Elephant is missing _id ! [
  { title: 'Lion', _id: new ObjectId("633c4d1f2441695e1b7dc3d5") },
  { title: 'Elephant' }
]

@hasezoey hasezoey added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 4, 2022
@vkarpov15 vkarpov15 added this to the 6.6.5 milestone Oct 4, 2022
vkarpov15 added a commit that referenced this issue Oct 4, 2022
vkarpov15 added a commit that referenced this issue Oct 5, 2022
fix(document): set defaults on subdocuments underneath init-ed single nested subdocument
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
3 participants