Read a collection

suggest change

Get all documents in the collection ‘myCollection’ and print them to the console.

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://localhost:27017/test';

MongoClient.connect(url, function (err, db) {
  if (err) throw new Error(err);
  var cursor = db.collection('myCollection').find(); // Read method 'find'
  cursor.each(function (err, doc) {
    if (err) throw new Error(err);
    if (doc != null) {
      console.log(doc); // Print all documents
    } else {
      db.close(); // Don't forget to close the connection when you are done
    }
  });
});

Collection method find()

db.collection(collection).find()

Argument| Type |Description| —— | — |—— |collection | string | A string specifying the collection

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents