MongoDB Commands


Data in MongoDB has a flexible schema.documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

The use Command

MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it doesn't exist otherwise it will return the existing database.

Syntax:
use DATABASE_NAME
Example:
>use mydb
	switched to db mydb
If you want to check your databases list, then use the command show dbs.
>show dbs
	local     0.78125GB
	test      0.23012GB
	

Drop Database

MongoDB db.dropDatabase() command is used to drop a existing database.

Syntax:
db.dropDatabase()
Example:
>show dbs
	local      0.78125GB
	mydb       0.23012GB
	test       0.23012GB
>use mydb
	switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }

Create Collection

MongoDB db.createCollection(name, options) is used to create collection. If u are known todatabase you can compare collection with table. Collection is something like a table.

Syntax:
db.createCollection(name, options)
Example: We have multiple option to create collection
>use test
   switched to db test
>db.createCollection("mycollection")
   { "ok" : 1 }

>db.createCollection("customer", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
   { "ok" : 1 }
>db.customer.insert({"name" : "tutorialspoint"})
>show collections
	mycol
	mycollection
	customer

Drop Collection

MongoDB's db.collection.drop() is used to drop a collection from the database.

Syntax:
db.COLLECTION_NAME.drop()
Example:
>show collections
	mycol
	mycollection
	customer

>db.mycollection.drop()
   true
>show collections
	mycol
	customer

Insert Document

To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method.

Syntax:
db.COLLECTION_NAME.insert(document)
Example:
>db.customer.insert({
   _id: ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'Soha',
   url: 'http://www.javavillage.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
})

Query Document

To query data from MongoDB collection, you need to use MongoDB's find() method.

Syntax:
db.COLLECTION_NAME.find()
Example:
>db.customer.find().pretty()
{
   "_id": ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'Soha',
   url: 'http://www.javavillage.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
}
AND in Query
>db.customer.find({"by":"Soha","title": "MongoDB Overview"}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'Soha',
   url: 'http://www.javavillage.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
}
OR in Query
>db.customer.find({$or:[{"by":"Soha"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'Soha',
   url: 'http://www.javavillage.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
}
AND and OR together in Query
>db.myccustomerol.find({"likes": {$gt:10}, $or: [{"by": "Soha"},
   {"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'Soha',
   url: 'http://www.javavillage.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
}

Delete Document

MongoDB's remove() method is used to remove document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag

Syntax:
db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example:
>db.customer.remove({'title':'MongoDB Overview'})
>db.customer.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
Remove All documents
>db.customer.remove()
Remove only one
>db.customer.remove(DELETION_CRITERIA,1)

Update Document

MongoDB's update() and save() methods are used to update document into a collection. The update() method update values in the existing document while the save() method replaces the existing document with the document passed in save() method.

Syntax:
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Example:
>db.customer.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
>db.customer.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}