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

前端 未结 8 881
青春惊慌失措
青春惊慌失措 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:07

    Found the official solution on: https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/

    And Mongoose also supports same chain.

    Bulk.find().upsert().update();
    Bulk.find().upsert().updateOne();
    Bulk.find().upsert().replaceOne();
    

    Tested it works:

    BulkWriteResult {
      result:
       { ok: 1,
         writeErrors: [],
         writeConcernErrors: [],
         insertedIds: [],
         nInserted: 0,
         nUpserted: 1,
         nMatched: 4186,
         nModified: 0,
         nRemoved: 0,
         upserted: [ [Object] ] } }
    

提交回复
热议问题