MongoDB Tutorial


MongoDB is an open source, NoSQL database that stores data in the form of documents. Being a NoSQL database, it is not relational and does not comply to ACID(Atomicity, Consistency, Isolation, Durability) properties.

  • Document - In mongoDB, data is stored in the form of documents, these are analogous to tuples/row in RDBMS. These documents contain data in the form of key-value pairs similar to JSON objects.
    { 
    	"_id" : ObjectId("5dfjsdd8d8fk3k9keb2d5d"), 
    	"FirstName" : "Kuldeep", 
    	"LastName" : "Rana", 
    	"Site" : "ArtOfTesting.com" 
    }
    

  • Collection - Collections are analogous to tables in RDBMS and are collection of of a set of related documents (but the documents can have dissimilar fields).

MongoDB Commands

  • mongo --port <port>-
    To connect to a mongoDB in a specified port

  • use <dbName>-
    To select a database <dbName<

  • show dbs-
    To display all the databases

  • show collections-
    To display all the collections in the current database

  • db.<collectionName>.find()-
    To select all the data in the specified collection

  • db.<collectionName>.find({<key>:<value>})-
    To fetch data in the specified collection with the given condition (key-value pair)

  • db.<collectionName>.insert(<document>)-
    To insert given document in the specified collection

  • db.<collectionName>.update({<key>:<value>}, <document>)-
    Updates data for the given key-value pair with the provided document/key-value pairs


For MongoDB Testing using Java, check our tutorial - MongoDB testing using Java.