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

337
Views
¿Cómo eliminar el nuevo ObjectId () de la visualización de datos JSON?

¿Cómo elimino el nuevo ObjectId() de mi salida cuando visualizo datos JSON?

A continuación se muestra mi código para obtener los datos usando la identificación de 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 restaurant with that id'; return res;

La salida es:

 { _id: new ObjectId("6157530bbba1dbb9f7e68e4a"), name: 'The Saffron Lounge', location: 'New York City, New York', phoneNumber: '123-456-7890', website: 'http://www.saffronlounge.com', priceRange: '$$$$', cuisines: [ 'Cuban', 'Italian' ], overallRating: 3, serviceOptions: { dineIn: true, takeOut: true, delivery: false } }

La salida que quiero es:

 { _id: "6157530bbba1dbb9f7e68e4a", name: 'The Saffron Lounge', location: 'New York City, New York', phoneNumber: '123-456-7890', website: 'http://www.saffronlounge.com', priceRange: '$$$$', cuisines: [ 'Cuban', 'Italian' ], overallRating: 3, serviceOptions: { dineIn: true, takeOut: true, delivery: false } }

¿Uso ObjectId.toString()? No estoy seguro de dónde usar esto tampoco

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

0

De acuerdo con esto, https://docs.mongodb.com/manual/reference/method/ObjectId.toString/ ObjectId.toString() le dará una cadena que aún contiene la parte ObjectId("...") . Podrías usar una expresión regular para eliminar eso.

 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 restaurant with that id'; res._id = res._id.toString().replace(/ObjectId\("(.*)"\)/, "$1") return res; }

about 4 years ago · Juan Pablo Isaza Report

0

res._id = res._id.str; return res;

https://docs.mongodb.com/manual/reference/method/ObjectId/

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!