8. RDBMS Vs MongoDB Terminology
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
Column Field
Table Join Embedded Documents
Primary Key
Primary Key (Default key _id provided
by mongodb itself)
9. Why Document ?
• Dynamic schema
• Elimination of object
relational mapping layer
• Implicit demoralization of
the data for performance.
10. Basic Operations In Mongo Shell
Commands Use
>use db_name
This command create database with the name mydb, but not show
newly created database in database list until unless it have at least
one document in it
>db This command shows currently selected database
>show dbs This command shows database list
>db.dropDatabase() Drop the database currently in use
11. Basic Operations In Mongo Shell
Commands Use
>db.createCollection(Na
me,Options)
This create a new collection in database which is currently under use
example
db.createCollection("mycol", { capped : true, autoIndexID : true, size :
6142800, max : 10000 } )
>db.COLLECTION_NAM
E.drop()
This command use to drop collection from database currently in use
12. Basic Operations In Mongo Shell
Commands Use
>db.COLLECTION_NAM
E.insert(document)
This command insert single data/document in specifient collection
and if collection is not already exist it will create one.
>db.COLLECTION_NAM
E.insert([document]) This command use to insert multiple documents, in the form of array
*if we don't specify the _id parameter, then MongoDB assigns a unique ObjectId for this
document.
*_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
13. Basic Operations In Mongo Shell
Commands Use
>db.COLLECTION_NAME.update(S
ELECTION_CRITERIA,
UPDATED_DATA)
This command updates the values in the existing
document
>db.COLLECTION_NAME.save({_id
:ObjectId(),NEW_DATA})
This command replaces the existing document with the
document passed in save() method.
>db.COLLECTION_NAME.remove(
DELLETION_CRITTERIA)
Remove all the documents with deletion criteria
>db.COLLECTION_NAME.remove(
DELETION_CRITERIA,1)
If there are multiple records and you want to delete only
the first record, then set justOne parameter in remove()
method.
14. Basic Operations In Mongo Shell
Commands Use
>db.mycol.remove()
If you don't specify deletion criteria, then MongoDB will
delete whole documents from the collection.
>db.mycol.find().pretty()
To query data from MongoDB collection, you need to use
MongoDB's find() method. To display the results in a
formatted way, you can use pretty() method.
>db.mycol.find( { $and: [ {key1:
value1}, {key2:value2} ] } ).pretty()
In the find() method, if you pass multiple keys by
separating them by ',' then MongoDB treats it
as AND condition.
>db.mycol.find( { $or: [ {key1:
value1}, {key2:value2} ] } ).pretty()
To query documents based on the OR condition, you
need to use $or keyword.
15. Basic Operations In Mongo Shell
Commands Use
>db.mycol.createIndex({"title":1,"des
cription":-1})
This command pass multiple fields, to create index on
multiple fields.
>db.COLLECTION_NAME.find({},{K
EY:1})
In MongoDB, when you execute find() method, then it
displays all fields of a document. To limit this, you need to
set a list of fields with value 1 or 0. 1 is used to show the
field while 0 is used to hide the fields.
>db.COLLECTION_NAME.createInd
ex({"title":1})
This command is use for giving index to specified field