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

379
Vistas
How do you replace and save documents in MongoDB?

I currently have a database that holds 5 entries per document in my mongoDB. Here is an example

I would like to update my documents in my database with more fields and information. Currently I am doing that with the code below.

    NFT.findOne({ "name": name }, (err, value) => {
        if (err) { console.error(err) }
        if (!value) {
            console.log('no value')
            let newNFT = new NFT({name, slug, symbol, description, verifiedStatus, bannerUrl, logoUrl, floorPrice, stats, social})
            newNFT.save() 
        } 
        else {
            let newNFT = new NFT({name, slug, symbol, description, verifiedStatus, bannerUrl, logoUrl, floorPrice, stats, social})
            NFT.replaceOne({"name": name}, newNFT, {returnDocument: "after"})
        }
    })

The reason for this question is, I have run console.log(NFT.find({"name": name}) and gotten an object back with all of the fields however, they don't seem to update in my database online. Because it pulls information from the web database, I know I'm connected, but they just aren't updating. What am I doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

save() returns a Promise, you should await it.
Plus, try to change the code for updating your existing value:

NFT.findOne({ name }, async (err, value) => {
  if (err) {
    console.error(err);
  }
  if (!value) {
    // Create new NFT
    let newNFT = new NFT({
      name,
      slug,
      symbol,
      description,
      verifiedStatus,
      bannerUrl,
      logoUrl,
      floorPrice,
      stats,
      social,
    });
    await newNFT.save();
  } else {
    // Update existing NFT
    value.name = name 
    value.slug = slug
    value.symbol = symbol
    value.description = description
    value.verifiedStatus = verifiedStatus
    value.bannerUrl = bannerUrl
    value.logoUrl = logoUrl
    value.floorPrice = floorPrice
    value.stats = stats
    value.social = social
    await value.save();
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar
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