简体   繁体   English

我如何 output 所有 collections 在 NodeJS (MongoDB Compass) 中使用 MongoDB

[英]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? 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().已经尝试过 find()。 But then I got something like this.但后来我得到了这样的东西。

在此处输入图像描述

The objects in MongoDB looks like this: MongoDB 中的对象如下所示:

在此处输入图像描述

 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()我认为您缺少toArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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