0% found this document useful (0 votes)
70 views4 pages

NGT

Here are the steps to insert documents using a for loop in MongoDB: 1. Connect to the MongoDB database: ```js mongoose.connect('mongodb://localhost/test'); ``` 2. Define the schema for the documents: ```js const schema = new mongoose.Schema({ name: String }); const Model = mongoose.model('Model', schema); ``` 3. Use a for loop to insert multiple documents: ```js for(let i = 0; i < 10; i++) { const doc = new Model({ name: 'Doc ' + i }); doc.save(function(err) {

Uploaded by

Anshuman Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views4 pages

NGT

Here are the steps to insert documents using a for loop in MongoDB: 1. Connect to the MongoDB database: ```js mongoose.connect('mongodb://localhost/test'); ``` 2. Define the schema for the documents: ```js const schema = new mongoose.Schema({ name: String }); const Model = mongoose.model('Model', schema); ``` 3. Use a for loop to insert multiple documents: ```js for(let i = 0; i < 10; i++) { const doc = new Model({ name: 'Doc ' + i }); doc.save(function(err) {

Uploaded by

Anshuman Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1. Explain the Capped Collection.

Ans Capped Collection

 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)

Option Type Type Description


Capped Boolean (Optional) If true, enables a capped collection. Capped
collection is a fixed size collection that automatically
overwrites its oldest entries when it reaches its
maximum size. If you specify true, you need to specify
size parameter also.
autoINDEXid Boolean (Optional) If true, automatically create index on _id
field. Deprecated since version 3.2

size Number (Optional) Specifies a maximum size in bytes for a


capped collection. If capped is true, then you need to
specify this field also.

max Number (Optional) Specifies the maximum number of


documents allowed in the capped collection.

Example: db.createCollection(“VSIT”,{capped:true, size:20000, max:100})

2)Discuss the various tools in MongoDB.

Ans MongoDB Tools

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.

Q3d. Explain the concept Inserting by Explicitly Specifying_id.

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.

 If the _id field was not specified, so it was implicitly added.

 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"

Q4)List and explain the 3 core components in the MongoDB package.

Ans. The core components in the MongoDB package are

1) mongod :which is the core database process

2) mongos : which is the controller and query router for sharded clusters

3) mongo : which is the interactive MongoDB shell

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.

Q5) Explain stand alone deployment and masterslave replication

Ans: used for deployment purpose,doesn’t ensure redundancy of data and recovery of data in case
of failure.

Not recommended for use in production environment.

Components of standalone deployment

Single mongoDB 2) client connecting to mongodb.

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.

6)List and explain the different conditional operators in MongoDB.

Ans. Different conditional operators are $lt , $lte , $gt , $gte , $in , $nin , and $not

1. $lt and $lte


They stand for “ less than ” and “ less than or equal to, ” respectively.
If you want to find all students who are younger than 25 (Age < 25), you can execute the
following find with a selector:
db.students.find({"Age":{"$lt":25}})
2. $gt and $gte The $ gt and $gte operators stand for “g reater than” and “ greater than or equal
to,” respectively.Let’s find out all of the students with Age > 25 . This can be achieved by
executing the following command:
db.students.find({"Age":{"$gt":25}})
3. $in and $nin Let’s find all students who belong to either class C1 or C2 . The command for the
same is db.students.find({"Class":{"$in":["C1","C2"]}}) The inverse of this can be returned by
using $nin . To find students who don’t belong to class C1 or C2 . The command is
db.students.find({"Class":{"$nin":["C1","C2"]}})

Q7) EXPLAIN INSERTING DOCUMENT USING FOR LOOP??

Ans:

You might also like