Learning Node.js MongoDB: Part 3: Reading

Learning Node.js MongoDB

MongoDB is a popular NoSQL document-oriented database system. The Node.js driver allows for connecting to an existing MongoDB installation or system.


Reading

MongoDB follows a design patterns of “one” or “many” for all primary operations. As used in the previous part, for example, insertion can use either insertOne() or insertMany(). Reading, using the find() function, follows this same pattern.

findOne()

The function findOne() operates on existing collections.

Reading works through supplying an object literal with any field to search for against existing records. The function will return an object containing all key-value pairs matching the search or null, if no results were found.

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  
    if (err) throw err;
  
    var dbo = db.db("example");
  
    dbo.collection("users").findOne({name: "Dan Cox"}, function(err, result) {
    
    if (err) throw err;
    
    // "result" will contain all key-value pairs or null
    
    db.close();

  });
});

find()

The function find() operates on existing collections.

Unlike findOne(), find() can return multiple results. However, instead of returning objects, the function returns a Cursor for iterating over possible results.

To help with converting the possible results into a more immediately useable form, the function toArray() can be used.

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  
    if (err) throw err;
  
    var dbo = db.db("example");
  
    dbo.collection("users").find({name: "Dan Cox"}).toArray(function(err, results) {
    
    if (err) throw err;
    
    // "results" will contain an array of results or be empty
    
    db.close();

  });
});

When used without any queries, find() will return ALL documents within a collection.

Sorting

The results from using find() can also be sorted using the sort() function. It accepts an object literal with the name of a field and then 1 (ascending) or -1 (descending).

As part of Cursor, sort() can also be chained with other functions like toArray() to sort before converting to an array.

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  
    if (err) throw err;
  
    var dbo = db.db("example");
  
    dbo.collection("users").find({name: "Dan Cox"}).sort({name: 1}).toArray(function(err, results) {
    
    if (err) throw err;
    
    // "results" will contain an array of results or be empty 
    
    db.close();

  });
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.