MongoDB Basics – A Quick Start Tutorial

December 15, 2012
By

In this tutorial, we will find quick answers to the some of the basic questions related with MongoDB database.

All the MongoDB statements given in this article is tested in MongoDB shell version 2.2.2.


Listing all the existing databases in MongoDB?

>show dbs

Creating a new MongoDB database?

>use university

The above command opens the database university , if it exists. Other wise a new database will be created with the name university, provided a new collection is created in it before exiting Mongo shell.


Exiting Mongo shell?

>exit

Showing currently opened database?

>db

Creating a new collection in MongoDB database?

Syntax

db.createCollection(name, { size : ..., capped : ..., max : ... } )

Example

>db.createCollection("students")

Inserting a document into a collection with insert() method?

Syntax

db.students.insert(obj)

Example

>db.students.insert(
... {
... name:"Rajesh",
... age:21,
... gender:"M",
... course:"BTECH",
... marks:81
... }
... )

Retrieving all the existing documents from a collection?

Syntax

db.students.find([query],[fields])

Example

>db.students.find()
{ "_id" : ObjectId("50cd99b62e85f03ba01adafb"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }

Inserting a document into a collection with save() method?

Syntax

db.students.save(obj)

Example

>db.students.save(
... {
... name:"John",
... age:21,
... gender:"M",
... course:"BTECH",
... marks:79
... })


Inserting multiple documents into a collection with single statement?

Syntax

db.students.insert(obj)

Example

>db.students.insert([
... { name:"Firoz", age:22,gender:"M",course:"BTECH",marks:88 },
... { name:"Alan", age:21, gender:"M", course:"BTECH", marks:68 },
... { name:"Dev", age:20, gender:"M", course:"MBA", marks:61 } ,
... { name:"Aishwarya", age:21, gender:"F", course:"MBA", marks:72 }
... ])

Retrieving all the existing documents from a collection?

Syntax

db.students.find([query],[fields])

Example

>db.students.find()
{ "_id" : ObjectId("50cd99b62e85f03ba01adafb"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cd99f22e85f03ba01adafc"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc57"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc58"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc59"), "name" : "Dev", "age" : 20, "gender" : "M", "course" : "MBA", "marks" : 61 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc5a"), "name" : "Aishwarya", "age" : 21, "gender" : "F", "course" : "MBA", "marks" : 72 }

Updating documents in a collection of MongoDB database?

Syntax


>db.students.update(query,object<,options>)

Example : Updating marks of Dev to 67

>db.students.update(
... { name:"Dev" },
... { $set:{marks:67} },
... { upsert:true, multi:true }
... )

Note :

If upsert is true, then new row will be inserted provided no matching document corresponding to query is found.

If multi is true, then multiple documents matching the query will be updated.


Deleting documents from a collection?

Syntax

db.collection.remove( <query>, <justOne> )

Example : Deleting all MBA students

>db.students.remove({course:"MBA"},false)

Note :

If <justOne> is true, then only one row will be deleted.

If <justOne> is false, then multiple rows will be deleted.


 Final list of documents in students collection

>db.students.find()
{ "_id" : ObjectId("50cebdcc984024d0901bad60"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cebe81984024d0901bad61"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50cebeccb73c3219ce6f4bb4"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50cebeccb73c3219ce6f4bb5"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }

How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner