You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
constmongoose=require("mongoose");constSchema=mongoose.Schema;constanimalSchema=newSchema({title: String,},{_id: true},);constanimalsSchema=newSchema({species: [animalSchema],totalAnimals: Number,});constUserschema=newSchema({animals: animalsSchema,});constUserModel=mongoose.model("UserAnimal",Userschema);(async()=>{awaitmongoose.connect(`mongodb://localhost:27017`,{dbName: "userSchema",});// init one docconstdoc=newUserModel();doc.animals={totalAnimals: 1};doc.animals.species=[{title: "Lion"}];awaitdoc.save();// once created we fetch it againconstuser=awaitUserModel.findById(doc._id);console.log({ user });// add new animaluser.animals.species.push({title: "Elephant"});awaituser.save();console.log("Elephant is missing _id !",user.animals.species);awaitmongoose.disconnect();})();
Expected Behavior
No response
The text was updated successfully, but these errors were encountered:
// NodeJS: 18.8.0// MongoDB: 5.0 (Docker)// Typescript 4.8.4import*asmongoosefrom'mongoose';// mongoose@6.6.3constSchema=mongoose.Schema;constanimalSchema=newSchema({title: String,},{_id: true});constanimalsSchema=newSchema({species: [animalSchema],totalAnimals: Number,});constUserschema=newSchema({animals: animalsSchema,});constUserModel=mongoose.model('UserAnimal',Userschema);(async()=>{awaitmongoose.connect(`mongodb://localhost:27017/`,{dbName: 'verifyMASTER',});// init one docconstdoc=newUserModel();doc.animals={totalAnimals: 1};doc.animals.species=[{title: 'Lion'}];awaitdoc.save();// once created we fetch it againconstuser=awaitUserModel.findById(doc._id);console.log(user);// add new animaluser.animals.species.push({title: 'Elephant'});awaituser.save();console.log('Elephant is missing _id !',user.animals.species);awaitmongoose.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' }
]
Prerequisites
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
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: