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

164
Views
Cómo usar updateOne en Mongodb usando NodeJS

A continuación se muestra mi código para actualizar un campo de sitio web en Mongodb usando NodeJS, en lugar de actualizar, sin embargo, agrega un nuevo campo a mi base de datos.

 let { ObjectId } = require('mongodb'); async get(id) { if (!id) throw 'You must provide an id to search for'; const restaurantsCollection = await restaurants(); const res = await restaurantsCollection.findOne({ _id: id }); if (res === null) throw 'No dog with that id'; res._id = res._id.toString().replace(/ObjectId\("(.*)"\)/, "$1") return res; } async rename(id, newWebsite) { if (!id) throw 'You must provide an id to search for'; if (!newWebsite) throw 'You must provide a name for the restaurant'; let newid = ObjectId(id); const restaurantsCollection = await restaurants(); const updatedInfo = await restaurantsCollection.updateOne( { _id: newid }, { $set: {website:newWebsite} } ); if (updatedInfo.modifiedCount === 0) { throw 'could not update restaurant successfully'; } return await this.get(newid); } };

A continuación se muestra la salida que obtengo:

 { _id: new ObjectId("6157825a4dcae5beb8f759bf"), name: 'Hey', location: 'New York City, New York', phoneNumber: '123-456-7890', website: 'http://www.hey.com', priceRange: '$$$$', cuisines: [ 'Cuban', 'Italian' ], overallRating: 3, serviceOptions: { dineIn: true, takeOut: true, delivery: false }, newWebsite: 'http://www.hi.com' }

En lugar de que el sitio web se reemplace de hey.com a hi.com, solo agrega un nuevo campo, ¿qué estoy haciendo mal?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En lugar de usar { $set: {website:newWebsite} }

Solo debería ser {website:newWebsite}

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!