Skip to content
cdhop edited this page Sep 4, 2014 · 13 revisions

Description

  • MongoDB is a BSON/JSON-based document database.
  • Each document is a JSON object.
  • 'Documents' are kept in 'collections', and a 'database' can have several 'collections'.
  • MongoDB enforces no schema, therefore each document may contain a unique set of fields/attributes (even if they are in the same collection).

Configuration

A MongoDB server's configuration is specified in the '/etc/mongodb.conf' file.

Export Data

One way to export data from MongoDB is the 'mongoexport' command. Here's an example:

$ mongoexport --host 127.0.0.1 --db mydb --collection customers > customers.json

Import Data

One way to import data into MongoDB is the 'mongoimport' command. Here's an example:

$ mongoimport -- host 127.0.0.1 --db mydb --collection customers --file customers.json

CRUD Operations

CRUD is an acronym for Create, Read/Retrieve, Update, and Delete (this basic operations of any database).

Create

The 'save' method/command is used to create documents. You include the text of a JSON document as the parameter. For example:

> db.collection.save({ title: "Sixth Column", author: "Heinlein"})

Read

The 'find' method/command is user to retrieve/read documents. This method/command can be simple and unconditional, or complex and specify criteria and fields to retrieve.

Here is a example of a simple use of the 'find' method/command:

> db.collection.find()

The problem with the output from this command is that it can be hard for a human to read. That is why I suggest appending the 'pretty' method. For example:

> db.collection.find().pretty()

Clone this wiki locally