0

Able to output one object based on the filter below, but in MongoDB there are 500 objects, so how do I output all the objects inside the collection of the MongoDB? Already tried find(). But then I got something like this.

enter image description here

The objects in MongoDB looks like this:

enter image description here

const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
async function run() {
  try {
    await client.connect();
    const database = client.db('database');
    const music = database.collection('music');
    const single = await music.find();
    console.log(single);
  } finally {
    await client.close();
  }
}

1 Answer 1

1

Can you try it like this:

const { MongoClient } = require('mongodb');
const client = new MongoClient(uri, { useUnifiedTopology: true }); //Options are optional
const db = await client.connect().db();
const results = await db.collection('music').find().toArray();
console.log(results);

I think you were missing toArray()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.