NGT
NGT
MongoDB has a concept of capping the collection. This means it stores the documents in the
collection in the inserted order.
As the collection reaches its limit, the documents will be removed from the collection in FIFO (first
in, first out) order. This means that the least recently inserted documents will be removed first.
This is good for use cases where the order of insertion is required to be maintained automatically,
and deletion of records after a fixed size is required.
One such use cases is log files that get automatically truncated after a certain size.
MongoDB itself uses capped collections for maintaining its replication logs. Capped collection
guarantees preservation of the data in insertion order, so queries retrieving data in the insertion
order return results quickly and don’t need an index.
Updates that change the document size are not allowed. Creating Capped Collection Syntax:
db.createCollection name, options)
Apart from the core services, there are various tools that are available as part of the MongoDB
installation:
• mongodump : This utility is used as part of an effective backup strategy. It creates a binary export
of the database contents.
• mongorestore : The binary database dump created by the mongodump utility is imported to a
new or an existing database using the mongorestore utility.
• bsondump : This utility converts the BSON files into human-readable formats such as JSON and
CSV. For example, this utility can be used to read the output file generated by mongodump.
• mongoimport , mongoexport : mongoimport provides a method for taking data in JSON , CSV, or T
SV formats and importing it into a mongod instance. mongoexport provides a method to export data
from a mongod instance into JSON, CSV, or TSV formats.
• mongostat , mongotop , mongosniff : These utilities provide diagnostic information related to the
current operation of a mongod instance.
Ans MongoDB stores data in documents. Documents are made up of key-value pairs. A key is used
for querying data from the documents.
Hence, like a RDBMS primary key, you need to have a key that uniquely identifies each document
within a collection. This is referred to as _id in MongoDB.
In the following example, you will see how to explicitly specify the id field when inserting the
documents within a collection.
While explicitly specifying the id field, you have to keep in mind the uniqueness of the field;
otherwise the insert will fail.
The following command explicitly specifies the id field: > db.users.insert({"_id":1, "Name": "vsit"})
The insert operation creates the following document in the users collection: { "_id" : 1, "Name" :
"vsit"
2) mongos : which is the controller and query router for sharded clusters
1. mongod The primary daemon in a MongoDB system is known as mongod . This daemon handles
all the data requests, manages the data format, and performs operations for background
management.When a mongod is run without any arguments, it connects to the default data
directory, which isC:\data\db or /data/db , and default port 27017, where it listens for socket
connections.It’s important to ensure that the data directory exists and you have write permissions to
the directory before the mongod process is started.
2. mongo mongo provides an interactive JavaScript interface for the developer to test queries and
operations directly on the database and for the system administrators to manage the database. This
is all done via the command line. When the mongo shell is started, it will connect to the default
database called test . This database connection value is assigned to global variable db
3. mongos mongos is used in MongoDB sharding. It acts as a routing service that processes queries
from the application layer and determines where in the sharded cluster the requested data is
located.
Ans: used for deployment purpose,doesn’t ensure redundancy of data and recovery of data in case
of failure.
Masterslave replication
In MongoDB, the traditional master/slave replication is available but it is recommended only for
more than 50 node replications. In this type of replication, there is one master and a number of
slaves that replicate the data from the master.
The only advantage with this type of replication is that there’s no restriction on the number of slaves
within a cluster. However, thousands of slaves will overburden the master node, so in practical
scenarios it’s better to have less than dozen slaves.
In addition, this type of replication doesn’t automate failover and provides less redundancy. In a
basic master/slave setup , you have two types of mongod instances: one instance is in the master
mode and the remaining are in the slave mode, as shown in Figure Since the slaves are replicating
from the master, all slaves need to be aware of the master’s address
The master node maintains a capped collection (oplog) that stores an ordered history of logical
writes to the database. The slaves replicate the data using this oplog collection.
Since the oplog is a capped collection, if the slave’s state is far behind the master’s state, the slave
may become out of sync.
In that scenario, the replication will stop and manual intervention will be needed to re-establish the
replication.
Ans. Different conditional operators are $lt , $lte , $gt , $gte , $in , $nin , and $not
Ans: