简体   繁体   中英

How do I output all collections with MongoDB in NodeJS (MongoDB Compass)

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.

在此处输入图像描述

The objects in MongoDB looks like this:

在此处输入图像描述

 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(); } }

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM