How to create collection and insert, delete and update data in mongoDB

In this tutorial I will explain you how to create collection and insert, delete and update data in mongoDB. after installing mongoDB read this tutorial How to install up MongoDB and PHP on Ubuntu
In mongodb you don’t need to create collection. MongoDB creates collection automatically, when you insert some document.

>db.mycollection.insert({"name" : "w3code"})
>show collections
mycol
mycollection
system.indexes
w3code
>

Still if you want to create the collection with commands you can.

MongoDB db.createCollection(name, options) is used to create collection.

Basic syntax of createCollection().

Run this command to create the collection.

db.createCollection(name, options)

In this command name is name of the collection that you want to create and Options is a document and used to specify configuration of collection

Now create a table with name mytable. Run the following command

>use mydatabase
switched to db mydatabase
>bd.createCollection("mytable")
{ "ok" : 1 }
>

The collection with name mytable has been created.Now run this command to check the created collection.

>show collections

MongoDB’s remove() method is used to remove document from the collection. Run this command to remove the document form mycollection

>db.mycollection.remove({'title':'w3code'}).
mytable 
system.indexs

MongoDB’s db.collection.drop() is used to drop a collection from the database. Now run this command

>use mydatabase
switched to db mydatabase
>show collections
mycol
mycollection
system.indexes
w3code
>

Now drop the collection with this command

>db.mycollection.drop()
true
>

Now check the list of the collection. if the selected collection is dropped successfully you will not see in the collection list.

To insert data in monogDB collection insert().Run this command to insert data in collection name mycollection

>db.mycollection.insert({
   _id: ObjectId(7df78ad8902c),
   title: 'w3code', 
   description: 'mycollection notes',
   by: 'w3code',
   url: 'http://www.w3code.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 1000
})

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. Run this command and see the output.

>db.mycollection.find().pretty()
{
   _id: ObjectId(7df78ad8902c),
   title: 'w3code', 
   description: 'mycollection notes',
   by: 'w3code',
   url: 'http://www.w3code.in',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 1000
}

The update() method updates values in the existing document.

Run this command to update the mongoDB collection

>db.mycollection.update({'title':'w3code'},{$set:{'title':'w3code new title'}})

By default mongodb will update only single document, to update multiple you need to set a paramter ‘multi’ to true.

>db.mycollection.update({'title':'w3code'},{$set:{'title':'w3code new title'}},{multi:true})

Hope this tutorial how to create collection and insert, delete and update data in mongoDB.