'What is the correct way to check for duplicate entries in Mongoose?
I'm fetching an API, and I want to store the result in my own DB. I need to check for duplicates, but my code did not find them.
Here is my code :
function parseAlertsReceived(JsonAlertArray) {
let alerts = []
let id, software, version
for (let i = 0; i < JsonAlertArray.length; i++) {
id = JsonAlertArray[i]['id']
if ( !isDuplicate(id) ) {
for (let j = 0; j < JsonAlertArray[i]['vulnerable_configuration'].length; j++) {
if (null != (JsonAlertArray[i]['vulnerable_configuration'][j])) {
software = getAlertSoft(JsonAlertArray[i]['vulnerable_configuration'][j])
version = getAlertVersion(JsonAlertArray[i]['vulnerable_configuration'][j])
alerts.push( new cveAlert(id, software, version) )
}
}
}
}
function isDuplicate(id_) {
cveAlertCollection.find({id: id_ } ).count()
.then( res => occurences = res)
.then(() => console.log( "Is it a duplicate id ? : " + occurences > 0 ))
return (occurences > 0)
}
With the same JsonAlertArray, I should have all entries added at the first update, then no entries if I call the function again since there all entries are already stored.
However, isDuplicate() always return false.
Here is a sample of the JsonArray : https://cve.circl.lu/api/last
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
