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
Add lodash docs explaining that you shouldn't deepClone() documents #12559
Comments
const cloneDeep = require('lodash/cloneDeep'); // fixed import
const mongoose = require('mongoose');
async function run() {
await mongoose.connect('mongodb://localhost:27017/test5');
const arr2Schema = new mongoose.Schema({ _id: false, prop1: String });
const arr1Schema = new mongoose.Schema({ _id: false, arr2: [arr2Schema] });
const Test = mongoose.model(
'Test',
new mongoose.Schema({
arr1: [arr1Schema],
field: String,
}),
);
let doc = new Test({ arr1: [{ arr2: [{ prop1: 'test' }] }] });
await doc.save();
const doc2 = await Test.findById(doc._id);
const newDoc = cloneDeep(doc2).set({ field: 'test' });
await newDoc.save(); // throws error
console.log(newDoc);
}
run().catch((err) => console.log(err)); |
I took a look and unfortunately we can't support We're planning on shipping a const doc2 = await Test.findById(doc._id);
const newDoc = doc2.$clone().set({ field: 'test' });
await newDoc.save(); // works
console.log(newDoc); In the meantime, since const doc2 = await Test.findById(doc._id);
const newDoc = new Test().init(doc2.toObject()).set({ field: 'test' });
await newDoc.save(); // works
console.log(newDoc); |
docs: add Lodash guide highlighting issues with cloneDeep()
Prerequisites
Mongoose version
6.6.5
Node.js version
16.15.1
MongoDB server version
4.4.13
Typescript version (if applicable)
No response
Description
In continuation to #6507. Lodash
cloneDeep
is now working perfectly when there is single level array subdocuments as mentioned in this comment but mongoose is still throwing following error when there is array of array subdocumentsSteps to Reproduce
Demo Code:
run this js script
Expected Behavior
Document should be updated without any error
The text was updated successfully, but these errors were encountered: