Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 we put in its own file and run using Node.js:

var mongo = require('mongodb').MongoClient;

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

var insert = function(collection, callback) {
  var documents = 
    [{ 
        call: 'kf6gpe-7', lat: 37.0, lng...