MongoDB - Learn MongoDB in A Simple Way! (PDFDrive)
MongoDB - Learn MongoDB in A Simple Way! (PDFDrive)
While all attempts have been made to verify the information provided in this book,
the author does assume any responsibility for errors, omissions, or contrary
interpretations of the subject matter contained within. The information provided in
this book is for educational and entertainment purposes only. The reader is responsible for
his or her own actions and the author does not accept any responsibilities for any liabilities
or damages, real or perceived, resulting from the use of this information.
The trademarks that are used are without any consent, and the publication of the
trademark is without permission or backing by the trademark owner. All
trademarks and brands within this book are for clarifying purposes only and are the
owned by the owners themselves, not affiliated with this document.
Introduction
MongoDB is a very powerful NoSQL database in terms of performance and the operations
that it can support. It is good for both database developers and programmers.
Programmers can link their MongoDB database with the various programming languages.
The MongoDB database was written in C++. It is among the leading NoSQL databases
today. One can achieve a lot in terms of a database with MongoDB. The database offers
We need to discuss the process by which one can create a three-member replica set from
three MongoDB existing instances which are executed with access control already
disabled.
When you have three member replica sets, you will ensure that there is enough
redundancy for enabling us to survive failures such as network partitions. They will also
provide us with enough capacity to cater to the distributed read operations. The number of
members in the replica sets should always be an odd number. This ensures that the
You just have to start the MongoDB instances that are to become members of the replica
set, perform a configuration of the replica set, and then have the instances of the
machines so as to ensure that there is much separation between the members. For those
using virtual machines for production deployments, each instance of the MongoDB has to
be placed in different host servers serviced by redundant network paths and redundant
power circuits.
The procedure
The following steps are necessary for deployment of a replica set when the access control
1. Use the appropriate options to start each member of your replica set
A mongod should be started for each member and the name of the replica set
specified through the option for “replSet.” For applications connecting to more than
one replica set, a distinct name should be specified for each set.
Let us use the option “replSet” to specify the name of our replica set:
The specification for this can also be done in the configuration file. For the mongod
to be started together with the configuration file, we can use the “—config” option
so as to specify this:
mongod —config $HOME/.mongodb/config
this process.
Assuming that you have your mongod running on localhost and on the port 27017,
Mongo
One should use the function “rs.initiate()”on their replica set member. This is
shown below:
rs.initiate()
MongoDB will initiate a set having the current member, and it will use the default
The displaying of the replica set configuration object can be done by use of the
method “rs.conf().” The configuration object for the replica set is as follows:
“_id” : “rs0”,
“version” : 1,
“members” : [
{
“_id” : 1,
“host” : “mongodb0.example.net:27017”
This can be done by use of the method “rs.add().” Consider the following example,
which shows how two members can be added:
rs.add(“mongodb0.example.net”)
rs.add(“mongodb1.example.net”)
After completion, you will have a fully functional replica set. It will then elect the
primary.
rs.status()
Chapter 3- Adding members to a Replica
Set
An additional member can be added to a replica set which is already in existence. Note
that each replica set should have a maximum of seven voting members. For one to add a
new member to a replica set which has seven members, you have to add the new member
In the case of production deployments, an “init script” can be configured for the purpose
A member who has been removed from the replica set can be re-added. If the removal of
the member had been done recently, the member can be recovered and catch up easily.
For those having a snapshot or a backup of an existing member, the data files can be
moved to a new system and then quickly used for initiation of a new member. The files
have to be:
1. A copy of data files which are valid for a
member belonging to the same replica set.
oplog. The new member has to become the current one once the operations from
Requirements
your network.
The Procedure
Before we can add the new member to the replica set, we have to prepare the data
directory for the new members. You can use any one of the following strategies:
members has no data. Data will be copied from an existing member by the new
member. For a new member in a recovering state, it has to exit and then become a
secondary before all the data can be copied by MongoDB as part of the replication
process. Note that some time will be required for this process to take place, but the
manually from the existing member. The new member will become a secondary
member, and it will catch up to the current state of our replica set. When we copy
the data, the amount of time required for the new member to become the current
one will be shortened.
Ensure that you are in a position to copy the data directory to the new member, and
then begin the replication within the window which is allowed by the oplog.
Otherwise, an initial sync will have to be performed by the new instance, and the
1. Begin by launching an instance of the MongoDB. You can then specify the
replica set name and the data directory. Consider the example given below:
In the above example, we have specified both the replica set and the data
directory. The host name and the information about the port for the new
instance of MongoDB also has to be specified.
Also, the replica set and the data directory can be specified in the configuration
file “mongod.conf,” and then the following command used to start the instance
of mongod:
Members can only be added once you have established a connection to the
primary. If you are not aware of the primary member of the replica set, log in to
any of the members of the replica set and then execute the following command:
db.isMaster()
3. The method “rs.add()” can then be used for addition of a new member to
rs.add(“mongodb3.mydb.net”)
In the above example, we are adding a new member to the host
“mongodb3.mydb.net.” You can also specify the port number depending on the
rs.add(“mongodb3.mydb.net:27017”)
4. You can then verify to check whether the member has been added to the replica
set. This can be done using the following method so as to display the
rs.conf()
To view the status of your replica set, use the following method:
rs.status()
Configuration and Addition of a New Member
To add a new member to the replica set, you just have to pass the member document to the
method “rs.add().” Consider a situation in which we want to add a member with the
an _id of 1.
a configuration as hidden
The following command can be used for adding the above member:
In MongoDB, it is possible for us to remove a member from a replica set. There are
different procedures which can be used to do this. Let us discuss these methods.
Using rs.remove()
1. Begin by shutting down the MongoDB instance of the user that you are in need of
removing from the replica set. To do this, you just have to establish a connection
using the mongo shell, and then execute the method “db.shutdownServer().”
2. Establish a connection with the current primary of the replica set. To know your
3. The member can then be removed using the method “rs.remove().” There are two
rs.remove(“mongod3.mydb.net:27017”)
rs.remove(“mongod3.mydb.net”)
The shell will be briefly disconnected by the MongoDB as the replica set continues
to elect the new primary. It will then be reconnected automatically. Note that even
if the command succeeds, the shell will have to display the error
One can remove a member from the replica set by editing the replica set configuration
1. Begin by shutting down the instance of the mongod for the member you want to
remove from the replica set. Once you have used the mongo shell to connect,
2. Establish a connection to the current primary of the replica set. If you are not aware
of the primary, connect to any member of the replica set and then execute the
method “db.isMaster().”
3. View the current document with the configuration details by executing the method
“rs.conf(),”and then determine from the array the position of the members that you
{
“_id” : “rs”,
“version” : 7,
“members” : [
“_id” : 0,
“host” : “mongod_A.mydb.net:27017”
},
“_id” : 1,
“host” : “mongod_B.mydb.net:27017”
},
“_id” : 2,
“host” : “mongod_C.mydb.net:27017”
4. The current configuration document can then be assigned to the variable “cfg” as
shown below:
cfg = rs.conf()
5. Remove the member by modifying the object “cfg.” Suppose you need to remove
cfg.members.splice(2,1)
6. You can then overwrite the configuration document of the replica set by issuing the
following command:
rs.reconfig(cfg)
After executing the above command, the replica set will be disconnected as the
negotiation for the new primary is being done. After that, the reconnection will be
done automatically. Even after the operation runs successfully, the error
“DBClientCursor::init call() failed” will be displayed so you don’t have to worry
about this.
7. You can then confirm the new configuration by executing the command
“_id” : “rs”,
“version” : 8,
“members” : [
“_id” : 0,
“host” : “mongod_A.mydb.net:27017”
},
“_id” : 1,
“host” : “mongod_B.mydb.net:27017”
}
Chapter 5- Replacing a Replica Set
Member
It is possible for one to change the name of the host of a member of the replica set without
For the hostname of a replica set to be changed, the “host” field has to be modified. The
value of the field “_id” will not be changed even after reconfiguring the set.
For the name of the host to be changed to “mongo2.mydb.net,” for our replica set member
cfg = rs.conf()
cfg.members[0].host = “mongo2.mydb.net”
rs.reconfig(cfg)
Chapter 6- Setting the Priority for a
Member
This is responsible for determining the outcome of the election of the primary. It is mostly
used to influence this election in which some members are made to be more likely to
become the primary. The priority is set in the form of a number, and the one having a
Considerations
For the priorities to be modified, the members array contained in the replica configuration
object has to be updated. The index of the array begins at 0. The value of the array index
should not be confused with the member’s_id field of the replica set in the replica set. The
value for the priority has to range between 0 and 1000, and it can take a floating point
When a particular member is assigned a priority of 0, it will not be considered during the
election of the primary.
The adjustment of a priority should only be done during a scheduled maintenance period.
When this value has been reconfigured, it can force the current primary to be dropped, and
then an election sought. Before an election is carried out, all of the client connections
In the mongo shell, retrieve the configuration of the replica set by executing the
cfg = rs.conf()
This should be changed as the configuration has been done in the array having the
members. Consider the example given below:
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 2
With the configuration, the priority value for the first three members of the array
will be changed.
The new configuration can be set by using the method “rs.reconfig().” This is
shown below:
rs.reconfig(cfg)
With the above command, the configuration of the replica set will be updated using
the value of the configuration defined in the variable “cfg.”
Chapter 7- Changing the Oplog Size
The oplog is found internally as a capped collection, meaning that its size cannot be
modified during the normal operations. The size of the oplog needed is variable due to
some circumstances.
For this to be changed, one has to perform maintenance on the members of the replica set
in turn. This should involve stopping the instance of the MongoDB, and then starting a
standalone instance of it, modifying the size of the oplog, and then restarting the member.
Procedure
secondary.
2. The oplog should then be recreated with a new size, and an old oplog entry
Identify any one of the non-primary members of the mongod instance in your replica set,
db.shutdownServer()
Note that this is optional. The existing oplog should be backed up on a standalone instance
as shown below:
Begin by saving the last entry from your oplog. You can use the mongo shell to connect to
the instance, and then switch to the local database by executing the following command:
use local
db = db.getSiblingDB(‘local’)
The temp temporary collection should be empty, and this is why we have to drop the
db.temp.drop()
You should then find the last entry, and then save it in a temporary collection by use of the
method “db.collection.save()” and a resort on the reverse natural order. This is shown
below:
).limit(1).next() )
db.temp.find()
Removing the Oplog Collection in Existence
The old collection, that is, “oplog.rs,” should be removed from the local database. To do
db = db.getSiblingDB(‘local’)
db.oplog.rs.drop()
This can be done by use of the command “create,” and a new oplog of a new size will be
created. The size argument has to be specified in bytes. To create an oplog worth 2
gigabytes, the specification should be 2 * 1024 * 1024 * 1024. This is shown in the
command given below:
database.runCommand( { create: “oplog.rs”, capped: true, size: (2 * 1024 * 1024 *
1024) } )
If the above command runs successfully, you will get the following return:
{ “ok” : 1 }
The last entry which was saved previously in the old oplog can be inserted into the new
database.oplog.rs.save( database.temp.findOne() )
The following command can be used for confirming whether the entry has been inserted
db.oplog.rs.find()
Restarting the Member
The mongod should be restarted as a member of our replica set on its normal port. An
db.shutdownServer()
The member of the replica set will then recover and catch up before it can become eligible
This procedure should then be repeated for all of the members for whom you havethe need
of changing their oplog. It should also be repeated for the size of the oplog on the primary.
With the command interface for MongoDB, one can access all of the non CRUDE
operations. With the commands, one can accomplish various tasks such as initialization of
document, and the first key should be the name of the command. A good example of this
is the “isMaster” command which can be specified by using the following BSON
document:
{ isMaster: 1 }
Issuing Commands
With the mongo shell, a helper method named “db.runCommand(),” which can help us to
run commands. Consider the operation given below which can be used for running the
above command:
db.runCommand( { isMaster: 1 } )
Some commands have to be executed on the admin database. The commands will always
look as follows:
use admin
db.runCommand( {buildInfo: 1} )
However, there is a helper which executes the command automatically once in the admin
database. This is shown below:
db._adminCommand( {buildInfo: 1} )
Whenever any of the MongoDB commands has executed successfully, it returns a status
{ ‘ok’: 1 }
When a command has failed, it will return an ok message with a status value of 0.
Chapter 9- Data Recovery
Sometimes, the system might be shut down unexpectedly. In that case, the data on the data
will be left in an inconsistent state. For data consistency to be maintained, the database has
to be shut down in a clean manner. The durability journaling should also be used. With
MongoDB, data is written to the journal after every 100 milliseconds, and this makes it
If the execution of the instance is not done as part of the replica set, and journaling has not
been enabled, the following procedure can be used for the purpose of recovering data
which is in an inconsistent state. In case the execution is done as part of the replica set, the
restoration should be done from a backup or the instance of the mongod restarted with an
empty dbpath and then allow the MongoDB to do an initial sync so as to restore the data.
The option “mongod –shutdown” in MongoDB.
If you are aware of any instance of MongoDB which is running without journaling, and
then it stops unexpectedly and you were not using replication, then you have to run the
repair operation before you can start the MongoDB. If you were using replication during
the shutdown, then you just have to restore the database from a backup and then allow the
Note that the file named “mongod.lock” in the directory “/data/db” is not of size 0 bytes,
then your MongoDB will fail to start. If this is the case, then the following error message
Once you see the above error, just know that you have to run the MongoDB by use of the
“—repair” option. If the file “mongodb.lock” exists on the system and then you open the
database with the above option, or with the optional “—repairpath,” a message with the
In case you observe the above message, you can go ahead and remove the lockfile, and
then execute the repair operation before the database can be opened normally.
When your system suffers from an unexpected shutdown, then there are two ways on how
Use of the option “—repair” together with the option “—repairpath.” The
MongoDB will then read your data files which are existing, and the data found will
be written to the available data files. Before you can use this procedure, you will
not be required to remove the file “mongod.lock.”
By use of the “—repair” option. The mongod will have to read the data from
the existing files, and then this data will be written to new files, and the files in
existence which are corrupt will be replaced with the new files. The file
“mongod.lock” has to be removed before this procedure can be used.
The following procedure can be used for repairing the data files and then preserving the
original ones.
For the data files to be repaired by use of the “—repairpath” option, and for the original
data files to be preserved, the following steps are necessary:
1. Use the option to start the mongod for the original files to be replaced with
the repaired ones. Use the options “—repair” and “—repairpath” to start the
Once the process has completed, the data files which have been repaired will be
2. The MongoDB instance should then be started with the new data directory
The mongodb instance should be started with the dbPath pointed to “/data/db0.”
This is shown below:
Once you have confirmed and found that the new data files are fully operational,
the old ones can be deleted or archived in the directory “/data/db.” The files which
have been repaired can also be moved to the old database or the dbpath updated so
If we are not in need of preserving the old files during the repair of the data files, we do
not have to use the option “—repairpath.” The following steps can be followed:
rm /data/mydb/mongod.lock
The directory “/data/mydb” has to be replaced with the dbpath in which your
MongoDB original data files are located.
2. The MongoDB instance should then be started with the option for replacing
the original files with the repaired ones. The option “—repair” should be used for
this, and the original files will be replaced with the repaired ones. A command
Once the execution of the above command has been completed, the original files
The following command, which points to our dbPath “/data/mydb” should be used
With the database profiler, fine grained data can be collected, and this is always in terms
of write operations, database commands, and commands which are running on the
instance of the MongoDB. The profiling can be enabled in terms of either per-database or
per-instance. During the process of enabling profiling, we can also configure the profiling
level.
Once the database has collected data, it writes all of it to the collection “system.profile,”
which is just a capped collection.
1- Only data for the slow operations will be collected. The default setting for
slow operations are the ones which are below 100 milliseconds.
2- This will collect all the profiling data for the database operations.
The process of enabling data profiling can be done from the Mongo shell or through a
driver which makes use of the “profile” command. We will guide you on how to do this
via the Mongo shell.
When you have enabled the process of profiling, the profiling level will also be set. The
data about the profiling will be recorded in the collection named “system.profile.”
For one to enable profiling and then set the profiling level, the method
“db.setProfilingLevel()” should be used. This should be done in the mongo shell, and the
parameter passed should be the profiling level.
An example is when you need to set or enable profiling for all of your database operations.
This can be done as shown below:
db.setProfilingLevel(2)
After that, the shell will give you a document which shows your previous profiling level.
The value 1 and the “ok” are an indication that your command executed successfully.
When it comes to slow connections, the threshold will apply to all of your database
operations. When this has been changed, it is changed for all of the databases in the
instance.
Slow thresholds are usually below 100 milliseconds. If your database has a profiling level
For the threshold to be set, two parameters have to be passed to the helper
“db.setProfilingLevel()”in the mongo shell. The first parameter will be responsible for
setting the profiling level for the current database, and the second one will set it for the
slow operation and then used as the default level.
Consider the command given below which will set the profiling level to 0, and this will
disable the profiling. The slow operation threshold will also be set to 20 milliseconds. A
database which is on an instance of 1 will then use this threshold. The command is given
below:
db.setProfilingLevel(0,20)
This can be done on the mongo shell by executing the following command:
db.getProfilingStatus()
Once the above command has been executed, the shell will give you the following result:
The field “was” gives us the current profiling level. The field “slowms” will tell us about
how long an operation will exist for it to pass the “slow” threshold, and this is specified in
terms of milliseconds.
If you need to return only the profiling level, the helper “db.getProfilingLevel()” should be
used. This is shown below:
db.getProfilingLevel()
Disabling profiling
Profiling can be disabled by executing the following command in the mongo shell:
db.setProfilingLevel(0)
The profiling can also be enabled for an entire instance of MongoDB. This is usually the
case in development environments, and it facilitates the testing process. The profiling
level will apply to all the databases which have been provided by the instance of
MongoDB.
For profiling to be enabled for an instance of MongoDB, the following parameters should
With the above parameters, the profiling level will be set to 1, meaning that only data for
slow connections will be collected, and slow operations are the ones which will be
It is not possible for one to enable profiling for an instance of mongos. For you to be able
to enable profiling for a shard cluster, it has to be first enabled for each instance of
The data for the profiler can also be viewed. The collection “system.profile” provides us
with the database profiler logs, and this is all about our operations. For the profiling
information to be viewed, one has to query the above collection. One can also use
“$comment” for adding data to the query document and the process of analyzing data
from the profiler will be made much easier.
Suppose you need to get the 20 most recent log entries into the collection, a query similar
to the one given below can be executed:
db.system.profile.find().limit(20).sort( { ts : -1 } ).pretty()
For the purpose of returning all of the operations except the “$cmd”, that is, command
Operations associated with a certain collection can be returned by use of the query given
below:
With the above query, we need to return the operations on the database “db” specifically
The following query can be executed to get the operations which are slower than 10
milliseconds:
If you are in need of obtaining information about operations which took place in a certain
db.system.profile.find(
ts : {
).pretty()
With the example given below, we will look at the time range, suppress the user field from
the output so as to make it easier for reading, and then the results are sorted based on the
db.system.profile.find(
ts : {
},
{ user : 0 }
).sort( { millis : -1 } )
When you enable profiling in your database, once you execute the helper “show profile”
on the mongo shell, you will get the 5 most recent operations which have taken at least 1
millisecond to be executed. You have to issue the command “show profile” on the mongo
show profile
Once profiling has been enabled, it will have an effect, though minor on the performance
of the system. The collection “system.profile” is a capped collection having a default size
of 1 gigabyte. With storage of this size, one can store several profiling documents. The
The size of the above collection can be changed by following the steps given below:
If you need to create this collection with a size of 3,000,000 bytes, the following sequence
of commands can be used on the mongo shell:
db.setProfilingLevel(0)
db.system.profile.drop()
db.setProfilingLevel(1)
Chapter 11- Rotating Log Files
When you use the option “—logpath” or with the setting “systemLog.path,” the mongos
and the mongod instances will report all of the activity and operations and then record
them in a log file. This will be done live. When the reporting to the log file is being done,
what happens is that the MongoDB will rotate the logfile in response to the command
“logRotate” or after the mongos or mongod has received a signal of type SIGUSR1 from
The standard MongoDB log rotation process will archive the current log file and then start
a new one. The mongos or mongod will rename the current logfile by appending a UTC
timestamp to the name of the file, and this is done in ISODate format. A new log file will
be opened, the old log file closed, and all of the new log entries will be sent to your new
log file.
We need to demonstrate how the default log rotation takes place in MongoDB. This can be
done as follows:
Use a separate terminal, and then list the matching files as follows:
ls /var/log/mongodb/myserver.log*
This can be done by issuing the command “logRotate” in the mongo shell. It should
also be from the admin database. This is shown below:
use admin
db.runCommand( { logRotate : 1 } )
You can now view the log files which have been newly created. The command
ls /var/log/mongodb/myserver.log*
Two log files should be listed, that is, “server1.log” and “server1.log.
<timestamp>.” The latter is the renamed log file, while the former is the log file
created by mongos or mongod after it reopened the log file. When log files have
been rotated, the old ones which have been rotated are not modified. A timestamp
With this, the log file will be closed and then reopened by use of the typical log rotation
Note that the option “—logappend” has to be used with the “—logRotate reopen.”
Use a separate terminal, and then list the matching files by use of the command
given below:
ls /var/log/mongodb/myserver.log*
4.
Use the “logRotate” command so as to rotate the log file on the mongo shell and
from the admin database. This is shown below:
use admin
db.runCommand( { logRotate : 1 } )
An external process should be used for renaming the log file, following the typical log
rotation behavior used in Linux/Unix.
Syslog Log Rotation
With this process, the log data is send to the syslog by MongoDB but it is not written to a
mongod –syslog
The “—logpath” should not be included. This is because it will lead to an error as
the option “—syslog” will tell the mongod to send the data to the syslog. In case
you are in need of specifying the facility level to be used when logging messages to
our syslog, the option “—syslogFacility,” or the configuration setting
“systemLog.syslogFacility.”
Use the default log rotation mechanism of your system so as to store and rotate the
log output.
It is also possible for one to use “SIGUSR1” so as to force a log rotation to take
place. This is shown below:
There are some utilities provided together with MongoDB which can be used for
performing the process of backup and restoration of data. Note that these tools and
meaning that it can impact the performance of your database. They have the impact of
creating traffic for the database which is running, and forcing it to read all of its data
through the memory. When infrequently used data is read by MongoDB, the more
frequently used data can be supplanted, and this will have an effect of deteriorating the
With this tool, the contents of the local database will not be dumped. For all of the
databases to be backed up via the mongodump, one should hae the “backup” role. This
role will provide you with all the roles which are required for backing up the data.
For you to backup a particular database, you must have read access on it. There are several
roles which can provide you with this access, and this will include the “backup” role.
For the collection “systemprofile” to be backed up, whose creation is after activation of
database profiling, one must be having an additional read access on the collection. There
are several roles which can provide you with this access.
For one to backup the users and the user-defined roles, they must have a read access on the
admin database. This is because in MongoDB, all the information regarding the users and
their roles is stored in the admin database.
Consider a situation in which you want to backup data from an instance of mongos or
MongoDB. This is running on the same machine and on the default port, which is 27017.
The following command can be used for this purpose:
Mongodump
Mongodump version 2.2 and above uses a data format which is incompatible with the
earlier versions of the mongod. Recent versions of mongodump should not be used for
The host and the port of the instance of MongoDB which the mongodump is to connect to
Consider the example given below, which shows how a different output directory can be
specified:
For you to limit the amount of data which has been included in the database dump, the
options —db and –collection can be specified in the mongodump. An example of this is
given below:
The above command will create a dump of a collection, and give it the name “collection.”
This will be done from the database named “mydb” located in the “dump/” subdirectory
In case output files are found in the backup data folder, then mongodump will overwrite
them. Before executing the mongodump command severally, you should ensure that the
files you are having in the output folder are not needed anymore. Alternatives, the folders
or the files can be renamed.
It is also possible for us to create backups from non-local instances of mongod. This is
accomplished by specifying the “host” and “port” options for the command
“mongodump” which will help in backing up data contained in a remote host. Consider
the example given below, which shows how this can be accomplished:
This is used for restoring a Mongo database. However, for the restoration of the database
to be done, the user has to have a read/write access on each of the databases which is to be
restored. For the users and user-defined roles to be restored, one has to have access to the
admin database.
When you want to use this tool so as to establish a connection to an active mongos or
mongorestore dump-2015-09-17/
In the above case, the MongoDB will import the database backup from the directory 2015-
09-17 to the instance of mongod which is running on the local interface.
In case you had used the option “—oplog” to create your database so as to make sure there
is a point-in-time snapshot, then use the option “—oplogReplay” so as to call the
mongorestore –oplogReplay
During the insertion of items into the database, one can use the option “mongorestore –
objcheck” so as to check on the integrity of these items. The option “mongorestore –drop”
may also be used for dropping each collection from the database before performing a
The default setting is that the mongostore will establish a connection to an instance of
MongoDB which is executing on the localhost interface, that is, 127.0.0.1 and the default
port which is 27017. For the restoration to be done in a different host or port, then the
options “—host” and “—port” can be used. Consider the example given below:
In case authentication with the MongoDB is needed, you have to specify both the
username and the password for your database. This has been shown in the example given
above.
Import/Export
The tools mongoimport and mongoexport which are used in MongoDB assist in working
with the data which is in a human-readable format, which is either CSV or Extended
JSON format. If the data migration, either export or import, is complex, you might need to
come up with your own import and export scripts which you will use together with the
The commands copydb, clone, or cloneCollection can be used for the purpose of copying
a database from one instance to another, and they may be more suitable for certain tasks
than others. When using the mongo shell, you will be provided with the method
“db.copyDatabase().”
When exporting in the CSV format, the fields of the documents to export to have to be
specified. This involves specifying the “name” and the “address” fields to export. This is
shown below:
In this case, the fields can also be specified in a file having a list of fields separated by
lines. These should be the fields which are to be exported.
A good example is the “name” and “address” fields which can be specified in the file
“fileds.txt.” These are shown below:
name
address
You should then use the option “—fieldFile” so as to specify the fields which are to be
exported within the file. This is shown below:
It can be concluded that MongoDB is a very powerful NoSQL database which is in use
today. With MongoDB, both the software developers and database developers can perform
numerous operations, ranging from the simple to the complex ones. That is how powerful
the database is. It provides them with automatic scaling, good performance, and high
availability. This means that the user will experience the ease of use when working with
MongoDB.
The number of records or documents which can be supported by this database is also
large. Most of the operations in MongoDB are based on replica sets. The operations are
usually carried out on these replica sets. A replica set has a number of users, and one can
feel free to create and add users to it. The users can also be deleted from the replica set or
the information about them updated. This book has explored all of these processes and you
The data stored in the data files can also be backed up so as to cater to the circumstances
under which failures occur. There are various ways that one can perform a backup of their
Once you have performed a backup of your data and then a failure occurs, such as an
unexpected shutdown of your database, you will have to perform a recovery of your
database. The data files are always left in an inconsistent state once errors have occurred,
such as an unexpected shutdown. To bring it back to a consistent state, you have to
perform a recovery of the data. This book guides you on how to do this.