Answer the question
In order to leave comments, you need to log in
How to automate replacement of ObjectId with document?
const EmbeddedInformationSchema = Schema({
description: String
});
const UserSchema = Schema({
informations: [EmbeddedInformationSchema]
})
const EmbeddedInformation = mongoose.model('EmbeddedInformation', EmbeddedInformationSchema);
const User = mongoose.model('User', UserSchema);
const run = async ()=>{
await mongoose.connect('mongodb://localhost/test');
await mongoose.connection.db.dropDatabase();
await EmbeddedInformation.create({description: 'a'});
await EmbeddedInformation.create({description: 'b'});
let informationAll = await EmbeddedInformation.find();
let user = await User.create({informations: informationAll.map(item=>item._id)});
console.log(user);
}
run().catch(console.error)
{informations: [{description: 'a'}, {description: 'b'}]}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question