0% found this document useful (0 votes)
20 views15 pages

MongoDB Notes - Madhu

This document provides a comprehensive guide on how to work with MongoDB, including installation steps, commands for database and collection management, and key features of the NoSQL database. It explains the differences between SQL and NoSQL databases, outlines advantages of MongoDB, and includes important questions and answers about its functionality. Additionally, it covers topics such as indexing, sharding, and alternatives to MongoDB.

Uploaded by

madhu
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)
20 views15 pages

MongoDB Notes - Madhu

This document provides a comprehensive guide on how to work with MongoDB, including installation steps, commands for database and collection management, and key features of the NoSQL database. It explains the differences between SQL and NoSQL databases, outlines advantages of MongoDB, and includes important questions and answers about its functionality. Additionally, it covers topics such as indexing, sharding, and alternatives to MongoDB.

Uploaded by

madhu
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/ 15

MongoDB Notes

How to work with Mongo DB ?

Step 1 : Open Command prompt, Run as administrator

Step 2 : Give the MongoDB path


MongoDB Notes

Step 3 : Give mongod.exe and press enter

Step 4 : Now open another command prompt and Go to MongoDB path

Now we should be working in the MongoDB shell. To run the MongoDB shell, type the
following command:

Step 6 : Once you are in the MongoDB shell, create the database in MongoDB by typing this
command
MongoDB Notes

At any point if you want to check the currently connected database just type the command
db. This command will show the database name to which you are currently connected. This
is really helpful command when you are working with several databases.

To list down all the databases, use the command show dbs. This command lists down all the
databases and their size on the disk.
MongoDB Notes

Introduction to MongoDB :

MongoDB is a NoSQL database which stores the data in form of key-value pairs. It is an
Open Source, Document Database which provides high performance and scalability.

What is Document based storage?

A Document is nothing but a data structure with name-value pairs like in JSON. It is very
easy to map any custom Object of any programming language with a MongoDB Document.
For example : Student object has attributes name, rollno and subjects, where subjects is a
List.

Document for Student in MongoDB will be like :

name : "madhu kumar",

rollno : 1,

subjects : ["C Language", "C++", "Core Java"]

Key Features of MongoDB


MongoDB Notes

 Apart from most of the NoSQL default features, MongoDB does bring in some more,
very important and useful features :
 MongoDB provides high performance.
 MongoDB has a rich Query Language, supporting all the major CRUD operations.
 Auto Replication feature of MongoDB leads to High Availability.
 MongoDB supports multiple Storage Engines.
 When we save data in form of documents(NoSQL) or tables(RDBMS) who saves the
data? It's the Storage Engine. Storage Engines manages how data is saved in memory
and on disk.

Organizations that use MongoDB :

Below are some of the big and notable organizations which are using MongoDB as database
for most of their business applications.

 Adobe
 LinkedIn
 McAfee
 FourSquare
 eBay
 MetLife
 SAP

Difference between SQL AND NOSQL Database ?

SQL NO SQL
Relational database Non-relational database
Supports SQL query language Supports JSON query language
Table based Collection based and key-value pair
Row based Document based
Column based Field based

Advantages of MongoDB :
MongoDB Notes

 Create database :

Your created database (madhudb) is not present in list. To display database, you need to
insert at least one document into it.

How to clear MongoDB Shell screen : Ctrl + L


MongoDB Notes

Creating and Dropping Database :

Creating Collection(Nothing but creating Tables) :

MongoDB provides the insert () command to insert documents into a collection. The
following example shows how this can be done.
MongoDB Notes

Reading data from Collection :

Limit() and sort() functions in MongoDB :


MongoDB Notes

Count() and remove() functions in MongoDB :

Deleting documents from Collection – use remove() function


MongoDB Notes

Updating documents in a collection :

Creating and dropping collection :


MongoDB Notes

How to get particular fields(columns) :

MongoDB Regular Expressions :

 Pattern matching can be achieved by the $regex operator. This operator can be used
to find for certain strings in the collection.
 The ^ and $ symbol can be used for exact text searches with ^ being used to make
sure that the string starts with a certain character and $ used to ensure that the
string ends with a certain character.
 The 'i' along with the $regex operator can be used to specify case insensitivity so that
strings can be searched whether they are in lower case or upper case.
 The delimiters // can also be used for pattern matching.
MongoDB Notes
MongoDB Notes

MongoDB Most important Questions :

Explain what is MongoDB?

Mongo-DB is a document database which provides high performance, high availability and
easy scalability.

What is “Namespace” in MongoDB?

MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the
collection. The concatenation of the collection name and database name is called a
namespace.

What is sharding in MongoDB?

The procedure of storing data records across multiple machines is referred as Sharding. It is
a MongoDB approach to meet the demands of data growth. It is the horizontal partition of
data in a database or search engine. Each partition is referred as shard or database shard.

How can you see the connection used by Mongos?

To see the connection used by Mongos use db_adminCommand (“connPoolStats”);

Explain what is a replica set?

A replica set is a group of mongo instances that host the same data set. In replica set, one
node is primary, and another is secondary. From primary to the secondary node all data
replicates.

How replication works in MongoDB?

Across multiple servers, the process of synchronizing data is known as replication. It


provides redundancy and increase data availability with multiple copies of data on different
database server. Replication helps in protecting the database from the loss of a single
server.

While creating Schema in MongoDB what are the points need to be taken in
consideration?

Points need to be taken in consideration are

Design your schema according to user requirements

Combine objects into one document if you use them together. Otherwise, separate them

Do joins while write, and not when it is on read

For most frequent use cases optimize your schema


MongoDB Notes

Do complex aggregation in the schema

What is the syntax to create a collection and to drop a collection in MongoDB?

Syntax to create collection in MongoDB is db.createCollection(name,options)

Syntax to drop collection in MongoDB is db.collection.drop()

Explain what is the role of profiler in MongoDB?

MongoDB database profiler shows performance characteristics of each operation against


the database. You can find queries using the profiler that are slower than they should be.

Explain can you move old files in the moveChunk directory?

Yes, it is possible to move old files in the moveChunk directory, during normal shard
balancing operations these files are made as backups and can be deleted once the
operations are done.

To do safe backups what is the feature in MongoDB that you can use?

Journaling is the feature in MongoDB that you can use to do safe backups.

Mention what is Objecld composed of?

Objectld is composed of

Timestamp

Client machine ID

Client process ID

3 byte incremented counter

Mention what is the command syntax for inserting a document?

For inserting a document command syntax is database.collection.insert (document).

Mention how you can inspect the source code of a function?

To inspect a source code of a function, without any parentheses, the function must be
invoked.

What is the command syntax that tells you whether you are on the master server or not?
And how many master does MongoDB allow?

Command syntax Db.isMaster() will tell you whether you are on the master server or not.
MongoDB allows only one master server, while couchDB allows multiple masters.
MongoDB Notes

Mention the command syntax that is used to view Mongo is using the link?

The command syntax that is used to view mongo is using the link is
db._adminCommand(“connPoolStats.”)

Explain what are indexes in MongoDB?

Indexes are special structures in MongoDB, which stores a small portion of the data set in an
easy to traverse form. Ordered by the value of the field specified in the index, the index
stores the value of a specific field or set of fields.

Mention what is the basic syntax to use index in MongoDB?

The basic syntax to use in MongoDB is >db.COLLECTION_NAME.ensureIndex ( {KEY:1} ). In


here the key is the the name of the COLUMN (or KEY:VALUE pair) which is present in the
documents.

Explain what is GridFS in MongoDB?

For storing and retrieving large files such as images, video files and audio files GridFS is used.
By default, it uses two files fs.files and fs.chunks to store the file’s metadata and the chunks.

What are alternatives to MongoDB?

Cassandra, CouchDB, Redis, Riak, Hbase are a few good alternatives

You might also like