So basically I’m trying to get all the documents (as it’s called in Mongodb) in a collection. I’ve watched some Youtube videos and they use the db.collection.find() method to do this.
In my case however this doesn’t work for some reason. I’m using node js take a look at my code below.
import { MongoClient } from 'mongodb';
const client = new MongoClient(uri);
const database = client.db(dbase);
const collection = database.collection(collec);
const getAll = async () => {
const res = await collection.find({});
await client.close();
return res;
console.log(getAll);
I was expecting some sort of an object or maybe an array of objects but unfortunatly I get this:
<ref *1> FindCursor {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false,
[Symbol(client)]: MongoClient {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
I’m not going to show the full error message since it contains sesetive information but this is the first few lines which I think should be save to share.
Thanks in advance…
2
Answers
Found the answer
or
credit to Sou for the second answer!
You need to call the
toArray()
function to get the array of results.