Question: Expalin the Basic terminology for MongoDB?
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) |
Mysqld/Oracle |
mongod |
mysql/sqlplus |
mongo |
Note: # used for commenting.
Question: Create a New database?
use mydb #switched to db mydb
Question: Check which database you are currently using?
use
Question: Display the List of databases?
show dbs #All database will display which have atleast 1 document.
Question: Delete the current database?
db.dropDatabase() #Delete the current used database.
Question: How to create collection for a database?
db.createCollection("mycollection") #create a collection for current selected database.
Question: Display the List of collections in database?
show collections #All collection will display for current database.
Question: How to drop the collection?
db.mycollection.drop() #mycollection collection will be deleted.
Question: How to delete all the records from mongodb ?
db.collection.remove();
Question: How to delete all the records with condition?
db.collection.remove({uid=111});
Will delete all the record where uid=1111
Question: How to insert data(Know as document) into collection?
db.mycollection.insert({
_id: ObjectId(7df78ad89765),
title: 'MongoDB Overview',
by: 'Web technology',
tags: ['mongodb', 'database', 'NoSQL'],
})
#Single document is added in collection "mycollection" .
Question: How to add multiple document into collection in single command?
db.mycollection.insert({
{
title: 'MongoDB Overview',
by: 'Web technology',
tags: ['mongodb', 'database', 'NoSQL'],
},
{
title: 'MongoDB Overview2',
by: 'Web technology experts notes',
tags: ['mongodb', 'database', 'NoSQL' ,'Multiple Record'],
}
])
Question: What is command for search a document? Give Example?
find() is used to search. For Example
db.mycollection.find()#Search the one document in un structured way .
Question: How to search a document in pretty way (structured way) ? Give Example?
pretty() is used to search in pretty way. For Example
db.mycollection.find({"by":"Web technology"}).pretty()#Search the one document in structured way .
Question: How to search a document with "and condition"?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).pretty()#Search the one document in structured way .
Question: How to list first 10 document?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).limit(10).pretty()#Search the 10 document in structured way .
Question: How to get 2nd document?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).limit(1).skip(1).pretty()#Search the 10 document in structured way .
Question: How to list document with title ascending order?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).sort({"title":1}).pretty()#Search the in title ascending order.
Question: How to search document in title descending order?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).sort({"title":-1}).pretty()#Search the in title descending order.
Question: How to Add indexing?
db.mycollection.ensureIndex({"title":1,"description":-1})#title in ascending order and description in descending order.To create index in descending order you need to use -1.
Question: How to search a document with "OR condition"?
db.mycollection.find({"by":"Web technology",$or[{"title": "MongoDB Overview"}]}).pretty()#Search the one document in structured way .
Question: How to update a document?
db.mycollection.update({'title':'MongoDB Overview'},{$set:{'title':'MongoDB text'}})#update "MongoDB Overview" with "MongoDB text " .
Question: How to delete a document?
db.mycollection.remove({'title':'MongoDB Overview'})#Delete the record where document is 'MongoDB Overview' .
Find all records
db.mycollection.find();
Display all the records in this collection.
Find all records and display in pretty way
db.mycollection.find().pretty();
Display all the records in this collection but presentable way.
Find all records with single condition (Age: 29)
db.mycollection.find({age:29});
Display all the records where age=29.
Find all records with multiple AND condition (Age: 29, Number:17)
db.mycollection.find({age:29, number:17});
Display all the records where age=29 and number=17
Find all records with multiple OR condition (Age: 29 OR Number:17)
db.mycollection.find({$or:[{age:29},{number:17}]});
Display all the records where age=29 OR number=17 (each of one).
Find all records with multiple OR condition (Age>28 OR Number:17)
db.mycollection.find({$or:[{age:{$gt:28}},{number:17}]});
Display all the records where age>29 and number=17
Find all records and display and one display column (name)
db.mycollection.find({},{name:1}).pretty();
Display all the name in this collections.
Find all records and display and two display column (name and number)
db.mycollection.find({},{name:1,number:1}).pretty();
Display all the name and number in this collections.
Limit the number of record
db.mycollection.find().limit(3).pretty();
Display only 3 records.
Display all the records except 1,2,3
db.mycollection.find().skip(3).pretty();
Skip first 3 records.