Before your Node.js application can do anything with a MongoDB instance, it must connect to it over the network.
How to do it...
The Node.js drivers for MongoDB contain all of the necessary network code to establish and break connections with MongoDB running on your local or remote machine.
You need to include a reference to the native driver in your code and specify the URL of the database to connect to.
Here's a simple example that connects to the database and promptly disconnects:
The MongoDB database organizes its documents in collections, which are typically groups of documents that are related in some way (such as representing the same kinds of information). Because of this, your primary interface to documents is through a collection. Let's see how to get a collection and add a document to it.
Tip
A collection is a little like a table in relational databases, but there's no imposition that all documents in a collection have the same fields or the same types for each field. Think of it as an abstraction you can use to group similar kinds of documents.
How to do it...
Here's a function that inserts two static items into the collection named documents in our test database, which we put in its own file and run using Node.js: