Академический Документы
Профессиональный Документы
Культура Документы
1. Introduction
MongoDB
- Document-oriented database (Key-Value Pair).
- Wherever Key is reffered (Consider it as column).
- Full-featured data store that is scalable, flexible, and fast.
- Main purpose is to make scale out easier.
- Table = Collecton :: Row = Document.
- Schema free (no predefined number of columns or datatype is needed).
- Schema free features gives developer lot of flexibility to work with evolving
data models.
- Features :- Indexing (Secondary, unique, compound, geospatial and variety of fast querie
s).
- Stored java script (Developers can store javascript functions and values on
the server side).
- Aggregation (Supports mapreduce and other aggregation tools)
- Fixed size collections (Capped collections are fixed in size and are useful
for certain type of data, such as logs).
- File storage (Supports easy to use protocol for storing large files and meta
data).
- Speed :- Uses binary wire protocol to interact with server (instead of protocol with
more overhead like 'HTTP/REST')
- Dynamic padding to documents and preallocate data files to trade extra space
usage for consistent performance
- Uses memory maaped files in the default storage engine, which pushes respons
ibility of memory management to OS
- It has dynamic query optimizer that remember fastest way to perform query.
- Whenever possible, the database server offloads processing and logic to the
client side
- Simple Administration: - Automatic fail over (Slave becomes master)
- Once cluster know that new node exists it automatically integrates and confi
gurte it.
- Server is design to handle configuration automatically (but allows user as w
ell to tweak setting).
================================================================================
===================================================================
2. Getting Started
General
- Document = Row
- Collection = Table
- MongoDB can have multiple databases and each of which can have its own collec
tions and permissions
- Comes with javascript shell, useful for admin and data manipulation
- Every document has a special key, "_id", that is unique across the document s col
lection.
Document
- It's a Key-Value pair.
e.g. : {"foo" : 3, "greeting" : "Hello, world!"}
- Keys in document are always string.
- . and $ are special properites. Read doc carefully.
- MongoDB is type+case sensitive.
e.g.: a) {"foo" : 3} <-> {"foo" : "3"} :: One has integer type value and oth
er has string type value.
b) {"foo" : 3} <-> {"Foo" : 3} :: One has "foo" starting with small f
and other has "Foo" starting with capital F.
Collections
- Collection = Table
- A collection is a group of documents. If a document is the MongoDB analog of
a row in a relational database, then a collection can
be thought of as the analog to a table.
- Schema-Free
-> both of the following documents could be stored in a single collection:
{"greeting" : "Hello, world!"} {"foo" : 5}
- Collection name should be UTF8 string, should not be NULL, should not be sta
rt with system.(a prefix reserved for system collection),
should not contain reserved charact "$".
SubCollection: Subcollections are a great way to organize data in MongoDB, and
their use is highly recommended. (Need to revisit)
Databases: MongoDB groups collections into databases. A single instance of Mong
oDB can host several databases.
- Each of which can be stored on different disk.
- Database name should be UTF8 string, should not be NULL, should not be start
with system.(a prefix reserved for system collection),
should not contain reserved charact "$",".","/","\","\0", should be all lowe
r case, length should be maximum 64bytes.
- Database name will actually end up as files on your file system.
- Reserved DB names (admin, local, config)
- admin: if user added to admin then it will inherits permissions of all dat
abases. shutdown and listing DB can run only from admin DB.
- local: this DB should not be replicated and should be used only for local
collections.
- config: When Mongo is being used in a sharded setup (see Chapter 10), the
config database is used internally to store information about the shards.
NameSpace: By prepending a collection s name with its containing database, you can
get a fully qualified collection name called a namespace
e.g.: For instance, if you are using the blog.posts collection in the cms dat
abase, the namespace of that collection would be cms.blog.posts.
- Namespaces are limited to 121 bytes in length.
Start Mongo Server
- $ ./mongod
- When run with no arguments, mongod will use the default data directory, /dat
a/db/ and port 27017.
- HTTP will listen to port 28017 (http://localhost:28017).
MongoDB Shell
- MongoDB comes with a JavaScript shell that allows interaction with a MongoDB
instance from the command line.
- $ /opt/mongoDB/bin/mongo
- x = 200
- x / 5;
- JavaScript libraries
- > Math.sin(Math.PI / 2);
- > new Date("2010/1/1");
- > "Hello, World!".replace("World", "MongoDB");
- UDF function
- function factorial (n) {
{"x" : 3.14}
As will this:
{"x" : 3}
* string: UTF8 characters
{"x" : "foobar"}
* symbol: This type is not supported by the shell. If the shell gets a symbol
from the database, it will convert it into a string.
* object id: An object id is a unique 12-byte ID for documents
{"x" : ObjectId()}
* date: Dates are stored as milliseconds since the epoch. The time zone is no
t stored.
{"x" : new Date()}
* Regular Expresssion: Documents can contain regular expressions, using JavaS
cript s regular expression syntax:
{"x" : /foobar/i}
* code: Documents can also contain JavaScript code:
{"x" : function() { /* ... */ }}
* binary data: Binary data is a string of arbitrary bytes. It cannot be manip
ulated from the shell.
* maximum value: BSON contains a special type representing the largest possib
le value. The shell does not have a type for this.
* minimum value: BSON contains a special type representing the smallest possi
ble value. The shell does not have a type for this.
* undefined:
{"x" : undefined}
* array:
{"x" : ["a", "b", "c"]}
* embedded document:
{"x" : {"foo" : "bar"}}
*** Numbers: MongoDB has three number types (4-byte integer, 8-byte integer,
and 8-byte float).
*** _id and objectIds: In a single collection, every document must have a uni
que value for "_id".
The "_id" key s value can be any type, but it defaults to
an ObjectId.
ObjectIds use 12 bytes of storage,
-====0 1 2 3 | 4 5 6 | 7 8 | 9 10 11
Timestamp | Machine | PID | Increment
-====The first four bytes of an ObjectId are a timestamp in
seconds since the epoch.
This allows for up to 2563 (16,777,216) unique ObjectI
ds to be generated per process in a single second.
*** Autogeneration of _id: if _id not presented when document inserted, one w
ill be generated automatically.
This can be handled by the MongoDB server but will
generally be done by the driver on the client side.
================================================================================
===================================================================
3. Creating, Updating, and Deleting Documents
Insert/Batch Insert: When you perform an insert, the driver you are using converts the data struct
ure into BSON, which it then sends to the database
Documents larger than 4MB (when converted to BSON) cannot be saved to the dat
abase.
for key>
Collection User has one document.
Document: {"_id" : ObjectId("4b253b067525f35f94b60a31"), "name" : "joe",
"age" : 30, "sex" : "male", "location" : "Wisconsin"}
Insert: joe = {"_id" : ObjectId("4b253b067525f35f94b60a31"), "name" : "j
oe", "age" : 30, "sex" : "male", "location" : "Wisconsin"}
db.users.insert(joe);
Update: db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")},
{"$push": {"fav book": "power of now"}});
db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")},
{"$push": {"fav book": "new earth"}});
"Ne Modifier" in conjuction with push ($ne): Add a value to an array only if the value is not already present.
Update: db.users.update({"fav book": {"$ne" : "new earth"}},{"$push":
{"fav book": "new earth"}});
"AddToSet" Modifier ($addToSet): Use "$addToSet" to prevent duplicates in array. (Execute below statem
ent twice.)
Update: db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")
}, {"$addToSet": {"fav book": "new earth"}})
"Each" Modifier in conjuction with addToSet($each) (Add multiple values
at once in array): You can use "$addToSet" in conjunction with "$each" to add multiple u
nique values.
Update: db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")
}, {"$addToSet": {"fav book": {"$each" : ["earth1", "earth2"]}}})
"Pop" Modifier ($pop): Use "$pop", which can remove elements from either end.
{$pop : {key : 1}} : To remove elements from end of array.
{$pop : {key : -1}} : To remove elements from begining of array.
Update: db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")
}, {"$pop" : {"fav book": -1}})
"Pull" Modifier ($pull): Use "$pull" to remove elements of an array that match the given crite
ria.
Update: db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")
}, {"$pull" : {"fav book": "earth1"}})
Positional array modification: Array uses base index as 0 and put arrayIndex after key.
Update: db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")
}, {"$set" : {"fav book.0": "abc"}})
Find position of array element using "$" and update particular positi
on (here avoding hardcoded array index).
db.users.update({"fav book" : "new earth1"}, { "$set" : {"fav book.$"
: "new earth"}});
Upsert: It follows syntax same as update, the third parameter to update specifies
===================================================================
8. Administrations
-> Starting and Stopping MongoDB: * Start: e.g.:
1) ./mongod
(It will
2) ./mongod
(It will
3) ./mongod
(It will
terface
<./mongod --nohttpinterfact>
to disallow server side javascript execute starts mongod wit
h noscripting
<./mongod --noscripting>
================================================================================
===================================================================