Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc.save() throw duplicate error after insertMany, if doc is from error.insertedDocs in ordered:false insertMany #9677

Closed
fabiel-leon opened this issue Dec 8, 2020 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@fabiel-leon
Copy link

doc.save() throw duplicate error after insertMany error insertMany(items,{ordered:false}) if doc is from error.insertedDocs

MongoError: E11000 duplicate key error collection: sharednews.news index: id dup key: { : ObjectId('5fcf184a7ad701402cdb49c7') }
at Function.create (/node_modules/mongodb/lib/core/error.js:57:12)
at toError (/node_modules/mongodb/lib/utils.js:123:22)
at /node_modules/mongodb/lib/operations/common_functions.js:265:39
at handler (/node_modules/mongodb/lib/core/sdam/topology.js:942:24)
at /node_modules/mongodb/lib/cmap/connection_pool.js:350:13
at handleOperationResult (/node_modules/mongodb/lib/core/sdam/server.js:558:5)
at MessageStream.messageHandler (/node_modules/mongodb/lib/cmap/connection.js:277:5)
at MessageStream.emit (events.js:310:20)
at processIncomingData (/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at MessageStream.Writable.write (_stream_writable.js:318:11)
at Socket.ondata (_stream_readable.js:695:22)
at Socket.emit (events.js:310:20)
at addChunk (_stream_readable.js:286:12) {
driver: true,
index: 0,
code: 11000
}

class News {
  // findByFullName() becomes a static
  static async shortMany(items) {
    let inserted = [];
    try {
      const insertResult = await this.insertMany(items, { ordered: false });
      inserted = insertResult; // works fine
    } catch ({ insertedDocs }) {
      console.log('inserting error');
      if (insertedDocs) {
        inserted = insertedDocs;  // in this docs .save() throw duplicate error
      }
    }
    console.log(inserted.lenght);
    const results = await async.eachSeries(inserted, async (item) => {
      try {
        const data = await bitly.shorten(item.link);
        item.short = data.link;
        return item.save();  // throw error if doc is from error.insertedDocs after insertMany
      } catch (error) {
        console.error('shorting', error);
      }
    });

    return inserted;
  }
}

ItemSchema.loadClass(News);

Do you want to request a feature or report a bug?

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

@vkarpov15 vkarpov15 modified the milestones: 5.11.6, 5.11.7 Dec 8, 2020
vkarpov15 added a commit that referenced this issue Dec 10, 2020
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants