MongoDB is a document-oriented database where data is stored in flexible JSON-like documents within collections, rather than rows in tables. Documents can contain various data types and dynamically vary in structure. The MongoDB database stores collections of documents and provides basic CRUD functions through its shell interface to create, read, update and delete documents through queries, indexes and special collection types.
2. 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.”
3. MongoDB is intended to be a general-purpose database so aside from creating, reading,
updating, and deleting data
Indexing
Aggregation
Special collection types
4. 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.
5. In JavaScript, for example, documents are represented as objects:
{
"greeting“ : "Hello, world!“
}
6. 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 }
7. 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
8. 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.
9. 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.”
10. {"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
11. Collection names may not contain the character 0 (the null character) because this delineates
the end of a collection name.
12. 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.
13. When run with no arguments, mongod will use the default data directory, /data/db/
(or datadb 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
14. Running the Shell
To start the shell, run the mongo executable:
$Mongo
MongoDB shell
version : 2.4 . 0
connecting to : test >
15. 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
16. A MongoDB Client
To see the database db is currently assigned to, type in db and hit Enter:
>db
Test
17. Basic Operations with the Shel
CRUD:
create, read, update, and delete to manipulate and view data in the shell.
18. 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.
19. The most common types are
Null
Dates
Arrays
Embedded documents
21. Creating, Updating, and
Deleting 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
22. 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:
23. 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
24. 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.