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

536
Vistas
How to select * from 'x' in mongodb?

I'm trying to receive all fields in my database in my API call

My code:

exports.objfields = async (req, res, next) => {
  try {
    var db = mongo.connection;  
    var objeto = req.headers.objeto;
    const result = db.db.collection(objeto).find();
    return res.status(200).send({message:result});
  } catch (error) {
    return res.status(401).send({ message: error.message });
  }
};

For results 200 it's returning me the following sentence "message": {"_events": {},"_eventsCount": 0} , how can I return the fields in the API call? (obs. I also tried find({}), but it returns the same thing)

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

0

I see two issues with your code:

  1. Are you sure it should be: db.db.collection and not db.collection ? Hard to tell without seeing your DB init code, but just double check that first.

  2. You need to use async/await to wait for MongoDB to return the result to your Node application:

const result = await db.collection(objeto).find();

find() returns a JS Promise, not a direct value immediately. For more details on async/await, you can check:

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await

If you end up with a Cursor object but cannot see the values, try to convert the Cursor into an array using toArray():

For example:

const cursor = collection.find({});
const allValues = await cursor.toArray();

https://docs.mongodb.com/drivers/node/current/fundamentals/crud/read-operations/cursor/#return-an-array-of-all-documents

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