如何在mongodb中添加多个相同类型的文档

时间:2020-05-13 07:39:11

标签: node.js mongodb typescript nestjs

我正在开发一个应用程序,该应用程序需要从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)
}

0 个答案:

没有答案