Add array values into MongoDB where element is not in array

后端 未结 1 738
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 05:45

In MongoDB, this is the simplified structure of my account document:

{
    \"_id\" : ObjectId(\"5a70a60ca7fbc476caea5e59\"),
    \"templates\" :         


        
相关标签:
1条回答
  • 2021-01-20 06:19

    You can try with bulkWrite operation in mongodb

    Account.bulkWrite(
      req.body.accountTemplates.map((data) => 
        ({
          updateOne: {
            filter: { _id: req.account._id, 'templates.name' : { $ne: data.name } },
            update: { $push: { templates: { $each : data } } },
            upsert : true
          }
        })
      )
    })
    
    0 讨论(0)
提交回复
热议问题