I am fetching data from an API (array of objects) and I want to then check in the database if any of the records exist, using the url as the basis for duplicates. When it comes to inserting the new records, I get this monogoDB error:
MongoBulkWriteError: E11000 duplicate key error collection: news.news
index: sourceUrl_1 dup key: { sourceUrl:
“https://example.com/article-abc123” }
try {
const covidDbArticles = await News.find({ category: "Coronavirus" });
const filterCovidDuplicates = covidData.data.news.filter(
(covidApiSource) =>
!covidDbArticles.some(
(covidDatabaseSource) =>
covidApiSource.link === covidDatabaseSource.sourceUrl
)
);
if (filterCovidDuplicates.length) {
try {
await News.insertMany(covidNewsObj);
} catch (err) {
console.log("Error inserting covid data: " + err);
}
}
} catch (err) {
console.log("saving data failed: " + err);
}