I am currently exploring nestjs and I am faced with a challenge. My goal is to retrieve a document from collection_1 and then store the same document into collection_2. I have tried using the $out aggregation, but found that I am only able to save one document in collection 2 at a time. Each time I attempt to save a second document, the first document disappears. I am seeking a solution to save every document from collection 1 into collection 2 without losing any data.
Here is the service code snippet:
async order(name){
const list = await this.usersmodel.find({name: name}).exec()
// return list
try{
if(list){
const x = await this.usersmodel.aggregate([
{ $match: { name: name } },
{$out:"payment"}
])
return "Data successfully saved in the payment collection"
}
}
catch(error){
return(error.message)
}
}
And here is the corresponding controller code:
@Post('orderdata')
async orderdata(@Body('name') name){
return this.usersService.order(name)
}