Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!