Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda