UEU Course 9306 7 - 0154 PDF
UEU Course 9306 7 - 0154 PDF
OLEH
MODUL PRAKTIKUM
Modul 2 Data Modelling, Create and Drop Database, and Create Collection
Modul 3 Drop Collection, Data Types, Insert Document, and Query Document
Database
Database is a physical container for collections. Each database gets its own set of files on
the file system. A single MongoDB server typically has multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A
collection exists within a single database. Collections do not enforce a schema. Documents
within a collection can have different fields. Typically, all documents in a collection are of
similar or related purpose.
Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic
schema means that documents in the same collection do not need to have the same set
of fields or structure, and common fields in a collection's documents may hold different
types of data.
The following table shows the relationship of RDBMS terminology with MongoDB.
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Mysqld/Oracle mongod
mysql/sqlplus mongo
2
MongoDB
Sample Document
Following example shows the document structure of a blog site, which is simply a comma
separated key value pair.
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
_id is a 12 bytes hexadecimal number which assures the uniqueness of every document.
You can provide _id while inserting the document. If you don’t provide then MongoDB
provides a unique id for every document. These 12 bytes first 4 bytes for the current
timestamp, next 3 bytes for machine id, next 2 bytes for process id of MongoDB server
and remaining 3 bytes are simple incremental VALUE.
3
MongoDB
2. MongoDB ─ Advantages
Any relational database has a typical schema design that shows number of tables and the
relationship between these tables. While in MongoDB, there is no concept of relationship.
No complex joins.
Tuning.
Uses internal memory for storing the (windowed) working set, enabling faster
access of data.
4
MongoDB
3. MongoDB ─ Environment
32-bit versions of MongoDB only support databases smaller than 2GB and suitable only
for testing and evaluation purposes.
Now extract your downloaded file to c:\ drive or any other location. Make sure the name
of the extracted folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-
[version]. Here [version] is the version of MongoDB download.
Next, open the command prompt and run the following command.
In case you have extracted the MongoDB at different location, then go to that path by
using command cd FOOLDER/DIR and now run the above given process.
MongoDB requires a data folder to store its files. The default location for the MongoDB
data directory is c:\data\db. So you need to create this folder using the Command Prompt.
Execute the following command sequence.
C:\>md data
C:\md data\db
If you have to install the MongoDB at a different location, then you need to specify an
alternate path for \data\db by setting the path dbpath in mongod.exe. For the same,
issue the following commands.
5
MongoDB
In the command prompt, navigate to the bin directory present in the MongoDB installation
folder. Suppose my installation folder is D:\set up\mongodb
C:\Users\XYZ>d:
D:\>cd "set up"
D:\set up>cd mongodb
D:\set up\mongodb>cd bin
D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"
This will show waiting for connections message on the console output, which indicates
that the mongod.exe process is running successfully.
Now to run the MongoDB, you need to open another command prompt and issue the
following command.
D:\set up\mongodb\bin>mongo.exe
MongoDB shell version: 2.4.6
connecting to: test
>db.test.save( { a: 1 } )
>db.test.find()
{ "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }
>
This will show that MongoDB is installed and run successfully. Next time when you run
MongoDB, you need to issue only commands.
6
MongoDB
In the above installation, 2.2.3 is currently released MongoDB version. Make sure to install
the latest version always. Now MongoDB is installed successfully.
Start MongoDB
sudo service mongodb start
Stop MongoDB
sudo service mongodb stop
Restart MongoDB
sudo service mongodb restart
mongo
MongoDB Help
To get a list of commands, type db.help() in MongoDB client. This will give you a list of
commands as shown in the following screenshot.
7
MongoDB
8
MongoDB
MongoDB Statistics
To get stats about MongoDB server, type the command db.stats() in MongoDB client.
This will show the database name, number of collection and documents in the database.
Output of the command is shown in the following screenshot.
9
MODUL 2
DATABASE OBJEK TER-DISTRIBUSI
MongoDB
4. MongoDB ─ Data Modelling
Data in MongoDB has a flexible schema.documents in the same collection. They do not
need to have the same set of fields or structure, and common fields in a collection’s
documents may hold different types of data.
Duplicate the data (but limited) because disk space is cheap as compare to compute
time.
Example
Suppose a client needs a database design for his blog/website and see the differences
between RDBMS and MongoDB schema design. Website has the following requirements.
Every post has the name of its publisher and total number of likes.
Every post has comments given by users along with their name, message, data-
time and likes.
In RDBMS schema, design for above requirements will have minimum three tables.
10
MongoDB
While in MongoDB schema, design will have one collection post and the following structure:
{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
}
]
}
So while showing the data, in RDBMS you need to join three tables and in MongoDB, data
will be shown from one collection only.
11
MongoDB
5. MongoDB ─ Create Database
Syntax
Basic syntax of use DATABASE statement is as follows:
use DATABASE_NAME
Example
If you want to create a database with name <mydb>, then use DATABASE statement
would be as follows:
>use mydb
switched to db mydb
>db
mydb
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display database, you need to
insert at least one document into it.
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In MongoDB default database is test. If you didn't create any database, then collections
will be stored in test database.
12
MongoDB
6. MongoDB ─ Drop Database
In this chapter, we will see how to drop a database using MongoDB command.
Syntax
Basic syntax of dropDatabase() command is as follows:
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will
delete default 'test' database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then dropDatabase() command would be
as follows:
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
>show dbs
local 0.78125GB
test 0.23012GB
>
13
MongoDB
7. MongoDB ─ Create Collection
Syntax
Basic syntax of createCollection() command is as follows:
db.createCollection(name, options)
Options parameter is optional, so you need to specify only the name of the collection.
Following is the list of options you can use:
14
MongoDB
While inserting the document, MongoDB first checks size field of capped collection, then it
checks max field.
Examples
Basic syntax of createCollection() method without options is as follows:
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections.
>show collections
mycollection
system.indexes
The following example shows the syntax of createCollection() method with few
important options:
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
15
MODUL 3
DATABASE OBJEK TER-DISTRIBUSI
MongoDB
8. MongoDB ─ Drop Collection
Syntax
Basic syntax of drop() command is as follows:
db.COLLECTION_NAME.drop()
Example
First, check the available collections into your database mydb.
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint>
>db.mycollection.drop()
true
>
>show collections
mycol
system.indexes
tutorialspoint
>
drop() method will return true, if the selected collection is dropped successfully, otherwise
it will return false.
16
MongoDB
9. MongoDB ─ Datatypes
String: This is the most commonly used datatype to store the data. String in
MongoDB must be UTF-8 valid.
Integer: This type is used to store a numerical value. Integer can be 32 bit or 64
bit depending upon your server.
Min/Max Keys: This type is used to compare a value against the lowest and
highest BSON elements.
Arrays: This type is used to store arrays or list or multiple values into one key.
Timestamp: ctimestamp. This can be handy for recording when a document has
been modified or added.
Date: This datatype is used to store the current date or time in UNIX time format.
You can specify your own date time by creating object of Date and passing day,
month, year into it.
Code: This datatype is used to store JavaScript code into the document.
17
MongoDB
10. MongoDB ─ Insert Document
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insert(document)
Example
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Here mycol is our collection name, as created in the previous chapter. If the collection
doesn't exist in the database, then MongoDB will create this collection and then insert a
document into it.
In the inserted document, if we don't specify the _id parameter, then MongoDB assigns a
unique ObjectId for this document.
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes
are divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3
bytes incrementer)
To insert multiple documents in a single query, you can pass an array of documents in
insert() command.
18
MongoDB
Example
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
To insert the document you can use db.post.save(document) also. If you don't
specify _id in the document then save() method will work same as insert() method. If
the save() method.
19
MongoDB
11. MongoDB ─ Query Document
In this chapter, we will learn how to query document from MongoDB collection.
Syntax
The basic syntax of find() method is as follows:
>db.COLLECTION_NAME.find()
Syntax
>db.mycol.find().pretty()
Example
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Apart from find() method, there is findOne() method, that returns only one document.
20
MongoDB
where by =
db.mycol.find({"by":"tutorials
Equality {<key>:<value>} 'tutorials
point"}).pretty()
point'
Greater
db.mycol.find({"likes":{$gte:50}}).pre where likes
Than {<key>:{$gte:<value>}}
tty() >= 50
Equals
AND in MongoDB
Syntax
In the find() method, if you pass multiple keys by separating them by ',' then MongoDB
treats it as AND condition. Following is the basic syntax of AND −
>db.mycol.find({key1:value1, key2:value2}).pretty()
Example
Following example will show all the tutorials written by 'tutorials point' and whose title is
'MongoDB Overview'.
21
MongoDB
For the above given example, equivalent where clause will be ' where by='tutorials
point' AND title = 'MongoDB Overview' '. You can pass any number of key, value pairs
in find clause.
OR in MongoDB
Syntax
To query documents based on the OR condition, you need to use $or keyword. Following
is the basic syntax of OR −
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by 'tutorials point' or whose title is
'MongoDB Overview'.
22
MongoDB
Example
The following example will show the documents that have likes greater than 100 and
whose title is either 'MongoDB Overview' or by is 'tutorials point'. Equivalent SQL where
clause is 'where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB
Overview')'
23
MODUL 4
DATABASE OBJEK TER-DISTRIBUSI
MongoDB
12. MongoDB ─ Update Document
MongoDB's update() and save() methods are used to update document into a collection.
The update() method updates the values in the existing document while the save() method
replaces the existing document with the document passed in save() method.
Syntax
The basic syntax of update() method is as follows:
>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Example
Consider the mycol collection has the following data.
Following example will set the new title 'New MongoDB Tutorial' of the documents whose
title is 'MongoDB Overview'.
By default, MongoDB will update only a single document. To update multiple documents,
you need to set a parameter 'multi' to true.
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
24
MongoDB
Syntax
The basic syntax of MongoDB save() method is −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
Following example will replace the document with the _id '5983548781331adf45ec7'.
>db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New
Topic",
"by":"Tutorials Point"
}
)
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Tutorials Point New Topic",
"by":"Tutorials Point"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>
25
MongoDB
13. MongoDB ─ Delete Document
Syntax
Basic syntax of remove() method is as follows:
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Consider the mycol collection has the following data.
Following example will remove all the documents whose title is 'MongoDB Overview'.
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>
26
MongoDB
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
>db.mycol.remove()
>db.mycol.find()
>
27
MongoDB
14. MongoDB ─ Projection
In MongoDB, projection means selecting only the necessary data rather than selecting
whole of the data of a document. If a document has 5 fields and you need to show only 3,
then select only 3 fields from them.
Syntax
The basic syntax of find() method with projection is as follows:
>db.COLLECTION_NAME.find({},{KEY:1})
Example
Consider the collection mycol has the following data
Following example will display the title of the document while querying the document.
>db.mycol.find({},{"title":1,_id:0})
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
{"title":"Tutorials Point Overview"}
>
Please note _id field is always displayed while executing find() method, if you don't want
this field, then you need to set it as 0.
28
MongoDB
15. MongoDB ─ Limit Records
Syntax
The basic syntax of limit() method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER)
Example
Consider the collection myycol has the following data.
Following example will display only two documents while querying the document.
>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
>
If you don't specify the number argument in limit() method then it will display all
documents from the collection.
Syntax
The basic syntax of skip() method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
29
MongoDB
Example
Following example will display only the second document.
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}
>
30
MODUL 5
DATABASE OBJEK TER-DISTRIBUSI
MongoDB
16. MongoDB ─ Sort Records
Syntax
The basic syntax of sort() method is as follows:
>db.COLLECTION_NAME.find().sort({KEY:1})
Example
Consider the collection myycol has the following data.
Following example will display the documents sorted by title in the descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"Tutorials Point Overview"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
>
Please note, if you don't specify the sorting preference, then sort() method will display
the documents in ascending order.
31
MongoDB
17. MongoDB ─ Indexing
Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan
every document of a collection to select those documents that match the query statement.
This scan is highly inefficient and require MongoDB to process a large volume of data.
Indexes are special data structures, that store a small portion of the data set in an easy-
to-traverse form. The index stores the value of a specific field or set of fields, ordered by
the value of the field as specified in the index.
Syntax
The basic syntax of ensureIndex() method is as follows().
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Here key is the name of the file on which you want to create index and 1 is for ascending
order. To create index in descending order you need to use -1.
Example
>db.mycol.ensureIndex({"title":1})
>
In ensureIndex() method you can pass multiple fields, to create index on multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
>
ensureIndex() method also accepts list of options (which are optional). Following is the
list:
32
MongoDB
33
MongoDB
18. MongoDB ─ Aggregation
Aggregations operations process data records and return computed results. Aggregation
operations group values from multiple documents together, and can perform a variety of
operations on the grouped data to return a single result. In SQL count(*) and with group
by is an equivalent of mongodb aggregation.
Syntax
Basic syntax of aggregate() method is as follows:
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example
In the collection you have the following data:
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
_id: ObjectId(7df78ad8902d)
title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 10
},
{
_id: ObjectId(7df78ad8902e)
34
MongoDB
Now from the above collection, if you want to display a list stating how many tutorials are
written by each user, then you will use the following aggregate() method:
Sql equivalent query for the above use case will be select by_user, count(*) from
mycol group by by_user.
In the above example, we have grouped documents by field by_user and on each
occurrence of by_user previous value of sum is incremented. Following is a list of available
aggregation expressions.
db.mycol.aggregate([{$group
Sums up the defined value from all : {_id : "$by_user",
$sum
documents in the collection. num_tutorial : {$sum :
"$likes"}}}])
35
MongoDB
db.mycol.aggregate([{$group
Calculates the average of all given values : {_id : "$by_user",
$avg
from all documents in the collection. num_tutorial : {$avg :
"$likes"}}}])
db.mycol.aggregate([{$group
Gets the minimum of the corresponding
: {_id : "$by_user",
$min values from all documents in the
num_tutorial : {$min :
collection.
"$likes"}}}])
db.mycol.aggregate([{$group
Gets the maximum of the corresponding
: {_id : "$by_user",
$max values from all documents in the
num_tutorial : {$max :
collection.
"$likes"}}}])
db.mycol.aggregate([{$group
Inserts the value to an array in the
$push : {_id : "$by_user", url :
resulting document.
{$push: "$url"}}}])
Pipeline Concept
In UNIX command, shell pipeline means the possibility to execute an operation on some
input and use the output as the input for the next command and so on. MongoDB also
supports same concept in aggregation framework. There is a set of possible stages and
each of those is taken as a set of documents as an input and produces a resulting set of
documents (or the final resulting JSON document at the end of the pipeline). This can then
in turn be used for the next stage and so on.
$match: This is a filtering operation and thus this can reduce the amount of
documents that are given as input to the next stage.
36
MongoDB
$skip: With this, it is possible to skip forward in the list of documents for a given
amount of documents.
$limit: This limits the amount of documents to look at, by the given number
starting from the current positions.
$unwind: This is used to unwind document that are using arrays. When using an
array, the data is kind of pre-joined and this operation will be undone with this to
have individual documents again. Thus with this stage we will increase the amount
of documents for the next stage.
37
MODUL 6
DATABASE OBJEK TER-DISTRIBUSI
MongoDB
19. MongoDB ─ Replication
Why Replication?
To keep your data safe
High (24*7) availability of data
Disaster recovery
No downtime for maintenance (like backups, index rebuilds, compaction)
Read scaling (extra copies to read from)
Replica set is transparent to the application
Replica set is a group of two or more nodes (generally minimum 3 nodes are
required).
In a replica set, one node is primary node and remaining nodes are secondary.
After the recovery of failed node, it again joins the replica set and works as a
secondary node.
38
MongoDB
Start the MongoDB server by specifying -- replSet option. Following is the basic
syntax of --replSet:
39
MongoDB
Example
mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
It will start a mongod instance with the name rs0, on port 27017.
Now start the command prompt and connect to this mongod instance.
In Mongo client, issue the command rs.initiate() to initiate a new replica set.
To check the replica set configuration, issue the command rs.conf(). To check the
status of replica set issue the command rs.status().
Syntax
The basic syntax of rs.add() command is as follows:
>rs.add(HOST_NAME:PORT)
Example
Suppose your mongod instance name is mongod1.net and it is running on port 27017.
To add this instance to replica set, issue the command rs.add() in Mongo client.
>rs.add("mongod1.net:27017")
>
You can add mongod instance to replica set only when you are connected to primary node.
To check whether you are connected to primary or not, issue the
command db.isMaster() in Mongo client.
40
MongoDB
20. MongoDB ─ Sharding
Sharding is the process of storing data records across multiple machines and it is
MongoDB's approach to meeting the demands of data growth. As the size of the data
increases, a single machine may not be sufficient to store the data nor provide an
acceptable read and write throughput. Sharding solves the problem with horizontal scaling.
With sharding, you add more machines to support data growth and the demands of read
and write operations.
Why Sharding?
In replication, all writes go to master node
Latency sensitive queries still go to master
Single replica set has limitation of 12 nodes
Memory can't be large enough when active dataset is big
Local disk is not big enough
Vertical scaling is too expensive
Sharding in MongoDB
The following diagram shows the sharding in MongoDB using sharded cluster.
41
MongoDB
Shards: Shards are used to store data. They provide high availability and data
consistency. In production environment, each shard is a separate replica set.
Config Servers: Config servers store the cluster's metadata. This data contains a
mapping of the cluster's data set to the shards. The query router uses this metadata
to target operations to specific shards. In production environment, sharded clusters
have exactly 3 config servers.
Query Routers: Query routers are basically mongo instances, interface with client
applications and direct operations to the appropriate shard. The query router
processes and targets the operations to shards and then returns results to the
clients. A sharded cluster can contain more than one query router to divide the
client request load. A client sends requests to one query router. Generally, a
sharded cluster have many query routers.
42
MongoDB
21. MongoDB ─ Create Backup
Syntax
The basic syntax of mongodump command is as follows:
>mongodump
Example
Start your mongod server. Assuming that your mongod server is running on the localhost
and port 27017, open a command prompt and go to the bin directory of your mongodb
instance and type the command mongodump
>mongodump
The command will connect to the server running at 127.0.0.1 and port 27017 and back
all data of the server to directory /bin/dump/. Following is the output of the command:
43
MongoDB
Following is a list of available options that can be used with the mongodump command.
Restore Data
To restore backup data MongoDB's mongorestore command is used. This command
restores all of the data from the backup directory.
Syntax
The basic syntax of mongorestore command is:
>mongorestore
44
MODUL 7
DATABASE OBJEK TER-DISTRIBUSI
MongoDB
22. MongoDB ─ Deployment
When you are preparing a MongoDB deployment, you should try to understand how your
application is going to hold up in production. It’s a good idea to develop a consistent,
repeatable approach to managing your deployment environment so that you can minimize
any surprises once you’re in production.
The best approach incorporates prototyping your setup, conducting load testing,
monitoring key metrics, and using that information to scale your setup. The key part of
the approach is to proactively monitor your entire system - this will help you understand
how your production system will hold up before deploying, and determine where you will
need to add capacity. Having insight into potential spikes in your memory usage, for
example, could help put out a write-lock fire before it starts.
mongostat
This command checks the status of all running mongod instances and return counters of
database operations. These counters include inserts, queries, updates, deletes, and
cursors. Command also shows when you’re hitting page faults, and showcase your lock
percentage. This means that you're running low on memory, hitting write capacity or have
some performance issue.
To run the command, start your mongod instance. In another command prompt, go
to bin directory of your mongodb installation and type mongostat.
D:\set up\mongodb\bin>mongostat
45
MongoDB
mongotop
This command tracks and reports the read and write activity of MongoDB instance on a
collection basis. By default, mongotop returns information in each second, which you can
change it accordingly. You should check that this read and write activity matches your
application intention, and you’re not firing too many writes to the database at a time,
reading too frequently from a disk, or are exceeding your working set size.
To run the command, start your mongod instance. In another command prompt, go
to bin directory of your mongodb installation and type mongotop.
D:\set up\mongodb\bin>mongotop
46
MongoDB
D:\set up\mongodb\bin>mongotop 30
Apart from the MongoDB tools, 10gen provides a free, hosted monitoring service,
MongoDB Management Service (MMS), that provides a dashboard and gives you a view of
the metrics from your entire cluster.
47
MongoDB
23. MongoDB ─ Java
Installation
Before you start using MongoDB in your Java programs, you need to make sure that you
have MongoDB JDBC driver and Java set up on the machine. You can check Java tutorial
for Java installation on your machine. Now, let us check how to set up MongoDB JDBC
driver.
You need to download the jar from the path Download mongo.jar. Make sure to
download the latest release of it.
Connect to Database
To connect database, you need to specify the database name, if the database doesn't exist
then MongoDB creates it automatically.
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
48
MongoDB
Now, let's compile and run the above program to create our database myDb as shown
below.
$javac ConnectToDB.java
$java ConnectToDB
Create a Collection
To create a collection, createCollection() method of com.mongodb.client.MongoDatabase
class is used.
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
//Creating a collection
database.createCollection("sampleCollection");
System.out.println("Collection created successfully");
}
}
49
MongoDB
Getting/Selecting a Collection
To get/select a collection from the database, getCollection() method of
com.mongodb.client.MongoDatabase class is used.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Creating a collection
System.out.println("Collection created successfully");
// Retieving a collection
MongoCollection<Document> collection = database.getCollection("myCollection");
System.out.println("Collection myCollection selected successfully");
}
}
50
MongoDB
Insert a Document
To insert a document into MongoDB, insert() method of com.mongodb.client.MongoCollection
class is used.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
51
MongoDB
.append("description", "database")
.append("likes", 100)
.append("url", "http://www.tutorialspoint.com/mongodb/")
.append("by", "tutorials point");
collection.insertOne(document);
System.out.println("Document inserted successfully");
}
}
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
52
MongoDB
// Retrieving a collection
MongoCollection<Document> collection =
database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
}
}
53
MongoDB
Update Document
To update a document from the collection, updateOne() method of
com.mongodb.client.MongoCollection class is used.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection myCollection selected successfully");
54
MongoDB
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
}
}
Delete a Document
To delete a document from the collection, you need to use the deleteOne() method of
the com.mongodb.client.MongoCollection class.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
55
MongoDB
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
while (it.hasNext()) {
System.out.println("Inserted Document: "+i);
System.out.println(it.next());
i++;
}
}
}
56
MongoDB
Dropping a Collection
To drop a collection from a database, you need to use the drop() method of the
com.mongodb.client.MongoCollection class.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
// Creating a collection
System.out.println("Collections created successfully");
// Retieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
// Dropping a Collection
collection.drop();
System.out.println("Collection dropped successfully");
57
MongoDB
}
}
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
58
MongoDB
Remaining MongoDB methods save(), limit(), skip(), sort() etc. work same as
explained in the subsequent tutorial.
59
DAFTAR PUSTAKA
P M. Tamer Özsu and Patrick Valduriez, (2011), Principles of Distributed Database Systems,
Third Edition, Springer Publishing ISBN 978-1-4419-8833-1