mongo db

25
MongoDB

Upload: raghu-nath

Post on 26-Jan-2017

462 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Mongo db

MongoDB

Page 2: Mongo db

MongoDB is a document-oriented database, not a relational one.

A document-oriented database replaces the concept of a “row” with a more flexible

model, the “document.”

Page 3: Mongo db

MongoDB is intended to be a general-purpose database so aside from creating, reading,

updating, and deleting data

Indexing

Aggregation

Special collection types

Page 4: Mongo db

MongoDB is made up of databases which contain collections. A collection is made up of documents.

Each document is made up of fields. Collections can be indexed, which improves lookup and sorting performance.

Finally, when we get data from MongoDB we do so through a cursor whose actual execution is delayed until necessary.

Page 5: Mongo db

In JavaScript, for example, documents are represented as objects:

{

"greeting“ : "Hello, world!“

}

Page 6: Mongo db

This simple document contains a single key, "greeting“ , with a value of "Hello,

world!“ . Most documents will be more complex than this simple one and often will

contain multiple key/value pairs:

{ "greeting“ :"Hello, world!","foo":3 }

Page 7: Mongo db

The keys in a document are strings. Any UTF-8 character is allowed in a key, with a few

notable exceptions:

Keys must not contain the character \0 (the null character). This character is used

to signify the end of a key

Page 8: Mongo db

The . and $ characters have some special properties and should be used only in certain

circumstances, as described in later chapters. In general, they should be considered reserved,

and drivers will complain if they are used inappropriately.

Page 9: Mongo db

MongoDB is type-sensitive and case-sensitive

Collections:

A collection is a group of documents.

Dynamic Schemas:

Collections have dynamic schemas . This means that the documents within a single col‐ lection can have any number of different “shapes.”

Page 10: Mongo db

{"greeting“ : "Hello, world!"}

{ "foo":5}

Naming:

A collection is identified by its name. Collection names can be any UTF-8 string, with

a few restrictions:

The empty string ("") is not a valid collection name

Page 11: Mongo db

Collection names may not contain the character \0 (the null character) because this delineates the end of a collection name.

Page 12: Mongo db

Getting and Starting MongoDB MongoDB is almost always run as a network server that clients can connect to and

perform operations on

$ mongod

mongod --help

for help and startup options.

Page 13: Mongo db

When run with no arguments, mongod will use the default data directory, /data/db/

(or \data\db\ on the current volume on Windows).

If the data directory does not already exist or is not writable, the server will fail to start. It is important to create the data directory (e.g., mkdir -p /data/db/ ) and to make sure your user has permission to write to the directory before starting MongoDB

Page 14: Mongo db

Running the Shell To start the shell, run the mongo executable:

$Mongo

MongoDB shell

version : 2.4 . 0

connecting to : test >

Page 15: Mongo db

The shell is a full-featured JavaScript interpreter, capable of running arbitrary JavaScript programs. To illustrate this, let’s perform some basic math:

>x

=200

200

>x/5;

40

Page 16: Mongo db

A MongoDB Client To see the database db is currently assigned to, type in db and hit Enter:

>db

Test

Page 17: Mongo db

Basic Operations with the Shel CRUD:

create, read, update, and delete to manipulate and view data in the shell.

Page 18: Mongo db

Data Types Basic Data Types

Documents in MongoDB can be thought of as “JSON-like” in that they are conceptually

similar to objects in JavaScript.

JSON is a simple representation of data: the specification can be described in about one paragraph (their website proves it) and lists only six data types.

Page 19: Mongo db

The most common types are Null

Dates

Arrays

Embedded documents

Page 20: Mongo db

Running Scripts with the Shel

Page 21: Mongo db

Creating, Updating, andDeleting Documents

Adding new documents to a collection

• Removing documents from a collection

• Updating existing documents

• Choosing the correct level of safety versus speed for all of these operations

Page 22: Mongo db

Inserting and Saving Documents Inserts are the basic method for adding data to MongoDB.

Batch Insert

Batch inserts allow you to pass an array of documents to the database.

Insert Validation

MongoDB does minimal checks on data being inserted:

Page 23: Mongo db

Removing Documents Now that there’s data in our database, let’s delete it:

Remove Speed

Removing documents is usually a fairly quick operation.

Updating Documents

Setting a Write Concern

Page 24: Mongo db

Querying Find method is used to perform queries in MongoDB.

Specifying Which Keys to Return

Sometimes you do not need all of the key/value pairs in a document returned.

Page 25: Mongo db

Cursors The database returns results from find using a cursor.