Thursday, April 9, 2020

MongoDB

MongoDB is a NoSql Document based batabase.

MongoDB stored data inside a Database. A database have Collections. Collections are equivalent to tables in an RDBMS. Collections in turn contains Documents.

MongoDB can be accessed by typing mongo in command line.

help command can be used to list Mongodb commands.


use command can be used to use or create a database.  If the database already exist the MongoDB will switch the context to the database. If the database doesn't exist MongoDB will create a new one.


createCollection() can be use to create a collection. 

db.insert() command  can be used to add a document to the collection. A document is a JSON object that is stored in the database as BSON, a binary format.

>db.Contacts.insert({"name": "Shalvin"}) 





db..find() command is used to select the data in a collection.

> db.Contacts.find()


Though I have specified only the name while inserting the document it automatically adds another field _id which is the primary key.

MongoDB is schema-less. 

> db.Contacts.insert({"name": "Plaito", "location": "Kochi"})





 count() function can be used to retrieve the number of documents in a collection.

> db.Contacts.find().count()







> db.Contacts.find[0]
> db.Contacts.find()[0]
{ "_id" : ObjectId("5e8f1762fadf026099d135e1"), "name" : "Shalvin" }
> db.Contacts.find()[1]
{
        "_id" : ObjectId("5e8f1b0afadf026099d135e3"),
        "name" : "Plaito",
        "location" : "Kochi"
}


No comments:

Post a Comment