{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/7306","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/7306/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/7306/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/7306/events","html_url":"https://github.com/Automattic/mongoose/issues/7306","id":388373175,"node_id":"MDU6SXNzdWUzODgzNzMxNzU=","number":7306,"title":"`insertMany` ignore duplicate","user":{"login":"atulmy","id":685786,"node_id":"MDQ6VXNlcjY4NTc4Ng==","avatar_url":"https://avatars3.githubusercontent.com/u/685786?v=4","gravatar_id":"","url":"https://api.github.com/users/atulmy","html_url":"https://github.com/atulmy","followers_url":"https://api.github.com/users/atulmy/followers","following_url":"https://api.github.com/users/atulmy/following{/other_user}","gists_url":"https://api.github.com/users/atulmy/gists{/gist_id}","starred_url":"https://api.github.com/users/atulmy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/atulmy/subscriptions","organizations_url":"https://api.github.com/users/atulmy/orgs","repos_url":"https://api.github.com/users/atulmy/repos","events_url":"https://api.github.com/users/atulmy/events{/privacy}","received_events_url":"https://api.github.com/users/atulmy/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-12-06T19:56:10Z","updated_at":"2018-12-13T08:40:30Z","closed_at":"2018-12-10T08:36:13Z","author_association":"NONE","body":"I've a collection say `Tweets` and each tweet has `_id` and another unique id `id_str` (added when creating schema).\r\n\r\nWhen running `insertMany`, is it possible to just ignore tweets with duplicate `id_str` without throwing error? I tried following which does not work and still throw error:\r\n\r\n```\r\ntry {\r\n  const tweets = fetchedFromApi()\r\n\r\n  await Tweet.insertMany(tweets, { ordered: false })\r\n} catch (error) {\r\n  console.log(error) // -> MongoError: write operation failed\r\n}\r\n```\r\n\r\nNode: `v10.14.0`\r\nMongoose: `v5.3.13`\r\nMongoDB: `v4.0.2`"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/446886203","html_url":"https://github.com/Automattic/mongoose/issues/7306#issuecomment-446886203","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/7306","id":446886203,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0Njg4NjIwMw==","user":{"login":"mhombach","id":34406120,"node_id":"MDQ6VXNlcjM0NDA2MTIw","avatar_url":"https://avatars0.githubusercontent.com/u/34406120?v=4","gravatar_id":"","url":"https://api.github.com/users/mhombach","html_url":"https://github.com/mhombach","followers_url":"https://api.github.com/users/mhombach/followers","following_url":"https://api.github.com/users/mhombach/following{/other_user}","gists_url":"https://api.github.com/users/mhombach/gists{/gist_id}","starred_url":"https://api.github.com/users/mhombach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhombach/subscriptions","organizations_url":"https://api.github.com/users/mhombach/orgs","repos_url":"https://api.github.com/users/mhombach/repos","events_url":"https://api.github.com/users/mhombach/events{/privacy}","received_events_url":"https://api.github.com/users/mhombach/received_events","type":"User","site_admin":false},"created_at":"2018-12-13T08:40:30Z","updated_at":"2018-12-13T08:40:30Z","author_association":"NONE","body":"@atulmy This issue came up in my head when doing bulkInsert myself now and i think what you were doing from the beginning on was technical correct.\r\nSetting `ordered: false` in the `insertMany` will insert ALL documents that are possible and \"skip\" those that throw an error. But when finished, the function itself will report/throw an error with all information on how many documents had been saved and how many not (see the docs: https://docs.mongodb.com/manual/reference/method/db.collection.insertMany ).\r\nSo... i guess you were just thinking \"it throws an error, so no documents were stored\" maybe? But infact setting `ordered: false` will NOT abort the insert if errors happen, it will just report them at the end. So this should be the exact thing you wanted, am i right? Can you check if this works and you maybe misread the error-message? :)"}}
{"actor":{"display_login":"mhombach"}}