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

122
Visualizações
How to read data in collection in MongoDB?

I am working on a social media app using Node.js and MongoDB. I've got a problem with reading the data in the collection. I want to get everything from the collection.

Router.js:

router.get('/feed', async (req,res) => {
    try{
        await db.client.connect()
        await db.feed(db.client)
        res.render("feed")
    }catch(err){
        console.log(err)
    }finally{
        db.client.close()
    }
})

Db.js

async function feed(client, res){
    let result = await client.db('secret_db').collection('secrets')
    console.log(result)
}

Output:

Collection {
  s: {
    db: Db { s: [Object] },
    options: {
      raw: false,
      promoteLongs: true,
      promoteValues: true,
      promoteBuffers: false,
      ignoreUndefined: false,
      bsonRegExp: false,
      serializeFunctions: false,
      fieldsAsRaw: {},
      writeConcern: [WriteConcern],
      readPreference: [ReadPreference]
    },
    namespace: MongoDBNamespace { db: 'secret_db', collection: 'secrets' },
    pkFactory: { createPk: [Function: createPk] },
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined,
      maxStalenessSeconds: undefined,
      minWireVersion: undefined
    },
    bsonOptions: {
      raw: false,
      promoteLongs: true,
      promoteValues: true,
      promoteBuffers: false,
      ignoreUndefined: false,
      bsonRegExp: false,
      serializeFunctions: false,
      fieldsAsRaw: {}
    },
    readConcern: undefined,
    writeConcern: WriteConcern { w: 'majority' },
    slaveOk: false
  }
}

Take into consideration that I don't have a problem with rendering the pages. The problem must be done inside the async function feed()

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to use find method of the collection to get the actual data from the DataBase. In your case try this.

 let result = await client.db('secret_db').collection('secrets').find({})
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