mongo db
TRANSCRIPT
MongoDB
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.”
MongoDB is intended to be a general-purpose database so aside from creating, reading,
updating, and deleting data
Indexing
Aggregation
Special collection types
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.
In JavaScript, for example, documents are represented as objects:
{
"greeting“ : "Hello, world!“
}
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 }
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
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.
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.”
{"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
Collection names may not contain the character \0 (the null character) because this delineates the end of a collection name.
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.
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
Running the Shell To start the shell, run the mongo executable:
$Mongo
MongoDB shell
version : 2.4 . 0
connecting to : test >
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
A MongoDB Client To see the database db is currently assigned to, type in db and hit Enter:
>db
Test
Basic Operations with the Shel CRUD:
create, read, update, and delete to manipulate and view data in the shell.
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.
The most common types are Null
Dates
Arrays
Embedded documents
Running Scripts with the Shel
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
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:
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
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.
Cursors The database returns results from find using a cursor.