mongo db
TRANSCRIPT
![Page 1: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/1.jpg)
MongoDB
![Page 2: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/5.jpg)
In JavaScript, for example, documents are represented as objects:
{
"greeting“ : "Hello, world!“
}
![Page 6: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/10.jpg)
{"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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/11.jpg)
Collection names may not contain the character \0 (the null character) because this delineates the end of a collection name.
![Page 12: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/16.jpg)
A MongoDB Client To see the database db is currently assigned to, type in db and hit Enter:
>db
Test
![Page 17: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/17.jpg)
Basic Operations with the Shel CRUD:
create, read, update, and delete to manipulate and view data in the shell.
![Page 18: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/19.jpg)
The most common types are Null
Dates
Arrays
Embedded documents
![Page 20: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/20.jpg)
Running Scripts with the Shel
![Page 21: Mongo db](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022081604/588927d71a28ab77528b66df/html5/thumbnails/25.jpg)
Cursors The database returns results from find using a cursor.