我正在开发一个应用程序,该应用程序需要从collection_1中获取数据并在collection_2中提交相同的数据。但是在提交已保存在collection_2中的同一文档时出现错误。假设我已经将doc_1保存在从collection_1中获取的collection_1中,并且同样地,我再次将doc_2保存在了collection_2中,现在我又需要将doc_1保存在collection_1中的collection_1中,但是我没有得到确切的输出,无法在doc_1时再次保存doc_1已存在于collection_2中。简而言之,我无法保存具有相同字段的多个文档。
服务代码:
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)
}
}
控制器代码:
@Post('orderdata')
async orderdata(@Body('name')name){
return this.usersService.order(name)
}