Creating a document in MongoDB using Node.js

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 ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.