-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
structuredClone ObjectId #13341
Comments
Unfortunately this is limitation with the If you want to clone a Mongoose document, we recommend using Mongoose documents' built-in If you absolutely need to use Here's how you can use the mongoose.ObjectId.set('transform', val => val.toString());
const schema = new Schema({
_id: 'ObjectId',
nested: {
id: 'ObjectId'
},
subdocument: new Schema({}),
documentArray: [new Schema({})]
}, { versionKey: false });
let Test = db.model('Test', schema);
const doc = new Test({
_id: new mongoose.Types.ObjectId('0'.repeat(24)),
nested: {
id: new mongoose.Types.ObjectId('1'.repeat(24))
},
subdocument: {
_id: new mongoose.Types.ObjectId('2'.repeat(24))
},
documentArray: [{ _id: new mongoose.Types.ObjectId('3'.repeat(24)) }]
});
assert.deepStrictEqual(doc.toObject({ transform: true }), {
_id: '0'.repeat(24),
nested: {
id: '1'.repeat(24)
},
subdocument: {
_id: '2'.repeat(24)
},
documentArray: [{ _id: '3'.repeat(24) }]
}); Here's how you'll be able to use const schema = new Schema({
_id: 'ObjectId',
nested: {
id: 'ObjectId'
},
subdocument: new Schema({}),
documentArray: [new Schema({})]
}, { versionKey: false });
let Test = db.model('Test', schema);
const doc = new Test({
_id: new mongoose.Types.ObjectId('0'.repeat(24)),
nested: {
id: new mongoose.Types.ObjectId('1'.repeat(24))
},
subdocument: {
_id: new mongoose.Types.ObjectId('2'.repeat(24))
},
documentArray: [{ _id: new mongoose.Types.ObjectId('3'.repeat(24)) }]
});
assert.deepStrictEqual(doc.toObject({ flattenObjectIds: true }), {
_id: '0'.repeat(24),
nested: {
id: '1'.repeat(24)
},
subdocument: {
_id: '2'.repeat(24)
},
documentArray: [{ _id: '3'.repeat(24) }]
}); |
Thank you very much for the answer, currently I use a forEach with a .toString() and that is valid for me. greetings |
feat(document): add flattenObjectIds option to toObject() and toJSON()
Prerequisites
Mongoose version
7.1.0
Node.js version
18.16.0
MongoDB server version
5.0.6
Typescript version (if applicable)
No response
Description
Hello, when you do a deep clone with structuredClone of an objectId, it stays in an empty object. I'm not sure if it has to do with mongoose or the node.js API
https://nodejs.org/dist/latest-v18.x/docs/api/all.html#all_globals_structuredclonevalue-options
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Thanks greetings.
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: