问题
I am working on an app where I need to fetch data from collection_1 and submit that same data in collection_2. But I got an error while I am submitting a same document which is already saved in collection_2. Suppose I have saved doc_1 in collection_2 which is fetched from collection_1 and similarly again I saved doc_2 in collection_2, Now again I need to save doc_1 in collection_2 from collection_1 but I didn't get the exact output, am unable to save doc_1 again while doc_1 is already present in collection_2. In short, I am unable to save multiple documents with the same fields.
Code of services:
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"}
{ $merge: { into: "payment",on:"_id",whenMatched: "merge" } }
])
return "data saved in payment collection"
}
}
catch(error){
return(error.message)
}
}
code of the controller:
@Post('orderdata')
async orderdata(@Body('name')name){
return this.usersService.order(name)
}
来源:https://stackoverflow.com/questions/61768900/how-to-add-multiple-same-type-of-documents-in-mongodb