How to add multiple same type of documents in mongodb

半腔热情 提交于 2020-05-17 07:46:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!