Cannot retrieve all entries from MongoDB collection

You are closing mongo connection before you get response from server. Move client.close(); inside toArray callback.

const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const dbName = 'toy_db';

tryMongo();

function tryMongo() {
  MongoClient.connect(url, (err, client) => {
    if (err) return console.log(err);
    console.log('Connected successfully to server');
    const db = client.db(dbName);
    const collection = db.collection('toy');
    collection.find().toArray((err, docs) => {
      if (err) {
        console.log(err);
      } else {
        console.log('found these:');
        console.log(docs);
      }
      client.close();
    });
  });
}