Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

382
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda