Sie sind auf Seite 1von 8

MONGODB

What is MongoDB?
MongoDB is NOSQL Database written in C++ language .It is open Source and it is Document
Oriented database that provides high performance, easy scalability and high availability.
MongoDB concept of Collections and Documents.

Advantages of NOSQL Database:-


1. Dynamic Schemas
2. Sharding
3. Replication
4. Integrated caching

Dynamic Schemas: - In Relational Database if we want save student data, we want create tables
and columns to it, this is called Defined schema. This will be easy when we have less data in our
tables, but what if we have millions of data. Migration to the updated schema would be a hectic
job. NOSQL Database this is Solved, there is not required defined schema in NOSQL Database.

Sharding: - In Sharding, large database into small partitioned, faster and easily manageable
database.
In Relational Database follows vertical architecture, where single serve holds the all data. In
Relational database sharding is not default, to achieve this we can put lot of effort. Because
integrity (Inserting/Updating), multiple tables joining etc.
In NOSQL Database Sharding is by default. No additional effort. It is automatically spread data
across the server. Fetch the data from server in faster

Replication: - Auto data Replication is also supported in NOSQL Database by default. Hence, one
DB server goes down, data is restore and its copy create another one in network.

Integrated caching: - Many NOSQL Database supports Integrated Caching. Where in demand
data stored in cache to make quire faster.

Database: - Database in MongoDB is nothing but Container for Collections. Collection is nothing
but Tables in RDBMS.

Collection: - Collection in MongoDB is Set of MongoDB Documents. Documents is nothing but


Rows in table of RDBMS.

Document: - Document in MongoDB is nothing but the set of key-value pairs. These documents
will have dynamic schema which means that the documents in the same collection do not need to
possess the same set of fields.

RDBMS MONGODB
Database Database
Tables Collections
Rows Documents
Column Field
Joins Embedded Documents
How to Run MongoDB in our System
Go to C: drive
Then
Go to Program Files
Then
Go to MongoDB Folder
Then
Hold the Shift Key and Right click the Mouse in that select the Open Command Window Here
Then
Type mongod.exe and click Enter
It will be show the error because there is C:data\db Folder is not presented Then we want
create Data folder in C: drive and then Data folder create db folder
Then again go to MongoDB folder in C: drive and then Hold the Shift Key and Right click the
Mouse in that select the Open Command Window Here
Then
Type mongo.exe
The connection is accepted
How to create Database in MongoDB

In MongoDB to Creating Database using use Command, if that database name is exist it is
switched to that DB Otherwise it created.
Database names can be almost any character in the ASCII range. But they can't contain an empty
string, a dot (i.e. ".") or " ".

Since it is reserved, "system" can't be used as a database name.

A database name can contain "$".

Syntax: - use database name and Press F5 button

If you check which DB is switched in MongoDB Using db Command


If you check existing Database in MongoDB Using show dbs command, here if database not
containing collections that database is not show.

How to create collection?


A collection may store a number of documents. A collection is analogous to a table of an RDBMS.
Collection names must begin with letters or an underscore.

A Collection name may contain numbers.

You can't use "$" character within the name of a collection. "$" is reserved.

A Collection name must not exceed 128 characters. It will be nice if you keep it within 80/90
characters.

Syntax: - db.createCollection(collection_name,<options>)

Other way
Syntax: db.collectionname.insert ({Document1})
Ex: - db.subjects.insert({subjectname: maths})
And then press F5 button, this is if the collection name is present then it will be insert this
documents otherwise collection will create and insert will done
How to drop a Database?
Syntax: db.dropDatabase() then press F5 the switched database will be dropped.

How to show the existing collections in present DB


Syntax: show collections
How to drop the collection
Syntax: db.collection name.drop ()

How to Insert Document into Collection


Syntax: db.collection name.insert({Document1})

How to insert multiple documents


Syntax: db.collection name.insert ([{Document1},{Document2},{Document3},..])

Collections with namespace Description

dbname.system.namespaces list of all namespaces

dbname.system.indexes list of all indexes

dbname.system.profile stores database profiling information

dbname.system.users list of users who may access the database

dbname.local.sources stores replica slave configuration data and


state
How to get a query
Syntax: db.collection name.find()

How to retry first Document


Syntax: db.collection.findOne()

How to rename the Collection


Syntax: db.Old collection name.renamecollection(New Collection name)

How to display the distinct values in MongoDB


Syntax: db.collection name.distinct(field)

How to retry document based on the AND condition


Syntax: db.collection name.find({field1,field2})
How to retry document based on the OR condition
Syntax: db.collection name.find({$or: [{field1}, {field2}]})

How to retry the documents based on AND & OR Condition


Syntax: db.collection name.find({field1, $or:[{ field2,field3}]})

How to apply Greater Than, Less Than, Not Equal in Condition


Greater Than & Greater Than Equal:
Syntax: db. collection Name.find({fieldname:{$gt:Value}
Syntax: db. collection Name.find({fieldname:{$gte:Value}
Less Than:
Syntax: db. collection Name.find({fieldname:{$lt:Value}
Syntax: db. collection Name.find({fieldname:{$lte:Value}

Equal:
Syntax: db.collection name.find({field name :{$eq:value}})

Not Equal:
Syntax: db. collection Name.find({fieldname:{$ne:Value}

How to calculate no.of Documents in Mongo DB


Syntax: db.collection name.count ()
How to update Document in MongoDB
We can update a document based criteria with or without and also we can update multiple
documents but we want use multi option because otherwise only one document is modify at a
time.

Multi documents update

How to use save () function?


The save () method is used for inserting and update. If the document is not contain _id field it
will call insert method otherwise update method call.

Das könnte Ihnen auch gefallen