1

我正在尝试在 MongoDB 中插入多条记录,对于少量记录(少于 1000 条)它可以正常工作。但是对于庞大的数据集,如果有任何重复值,它会引发错误。

如果有重复记录,有没有办法继续没有错误?

代码

    db.collection.insertMany(numberList, {
      ordered: false
    });]

错误

    { MongoError: E11000 duplicate key error collection: campaignDB.campaign_number index: id dup key: { : "5e673dc3a53cf3730568a19b_94715987119" }
    at Function.create (/Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/core/error.js:44:12)
    at toError (/Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/utils.js:150:22)
    at UnorderedBulkOperation.handleWriteError (/Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/bulk/common.js:1125:11)
    at resultHandler (/Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/bulk/common.js:501:23)
    at handler (/Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/core/sdam/topology.js:973:24)
    at wireProtocol.(anonymous function) (/Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/core/sdam/server.js:437:5)
    at /Users/waruna/Anuja/GitBuddika/Message_hub/MHNG-Backend/campaign-services/node_modules/mongodb/lib/core/connection/pool.js:420:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  name: 'BulkWriteError',
  driver: true,
  code: 11000,
  writeErrors:
   [ WriteError { err: [Object] },
     ... 899 more items ],
  result:
   BulkWriteResult {
     result:
      { ok: 1,
        writeErrors: [Array],
        writeConcernErrors: [],
        insertedIds: [Array],
        nInserted: 0,
        nUpserted: 0,
        nMatched: 0,
        nModified: 0,
        nRemoved: 0,
        upserted: [] } },
  [Symbol(mongoErrorContextSymbol)]: {} }

4

0 回答 0