Trying to do a bulk upsert with Mongoose. What's the cleanest way to do this?

前端 未结 8 891
青春惊慌失措
青春惊慌失措 2020-12-05 10:13

I have a collection that holds documents that contains three fields: first_name, last_name, and age. I\'m trying to figure out what query in Mongoose I can use to do a bulk

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 11:05

    I have released a small plugin for Mongoose that exposes a static upsertMany method to perform bulk upsert operations with a promise interface. This should provide a very clean way of doing bulk upserts with Mongoose, while retaining schema validation etc:

    MyModel.upsertMany(items, ['matchField', 'other.nestedMatchField']);
    

    You can find this plugin on npm or Github:

    https://github.com/meanie/mongoose-upsert-many https://www.npmjs.com/package/@meanie/mongoose-upsert-many

提交回复
热议问题