Objective of the Blog is to build a basic understanding about CRUD (Create, Update and Delete) operations on a Document-based or a NoSQL Database.
MongoDB is a NoSQL database, unlike RDBMS the schema of a NoSQL DB is flexible, developer need not to define the collection structure before pumping data into it.
MongoDB explanation in this blog will be a comparative study with respect to RDBMS especially Oracle DB.
Current article on MongoDB will cover below topics:
1. Create a Database (equivalent to creating a schema/user in Oracle)
2. Select documents from collection (similar to select in oracle)
3. Insert/Update/Delete data (DML operations in RDBMS).
Before moving to the CRUD, lets build an understanding on some terms and terminologies used in respect to MongoDB.
Nosql : Database-->Collections-->Documents
RDBMS : Database(Schema)-->Tables-->Records(Rows)
As I am firm believer in reverse engineering, so I will be going into depth of further concepts of MongoDB in upcoming blogs. Keeping the scope of this blog restricted.
So lets start with the tutorials:
1. show dbs:
2. use <mydb> : use <mydb> is to create a new db if doesn't exist else connect to existing one.
3. db.createCollection(<coll_name>) : create an empty collection with name given in parenthesis.
MongoDB is a NoSQL database, unlike RDBMS the schema of a NoSQL DB is flexible, developer need not to define the collection structure before pumping data into it.
MongoDB explanation in this blog will be a comparative study with respect to RDBMS especially Oracle DB.
Current article on MongoDB will cover below topics:
1. Create a Database (equivalent to creating a schema/user in Oracle)
2. Select documents from collection (similar to select in oracle)
3. Insert/Update/Delete data (DML operations in RDBMS).
Before moving to the CRUD, lets build an understanding on some terms and terminologies used in respect to MongoDB.
Nosql : Database-->Collections-->Documents
RDBMS : Database(Schema)-->Tables-->Records(Rows)
As I am firm believer in reverse engineering, so I will be going into depth of further concepts of MongoDB in upcoming blogs. Keeping the scope of this blog restricted.
So lets start with the tutorials:
1. show dbs:
- returns the all default/created databases.
2. use <mydb> : use <mydb> is to create a new db if doesn't exist else connect to existing one.
- use mymongo creates a new database named "mymongo"
- show dbs doesn't print newly created database because new db has no collections yet
- db return the current database user is connected to.
3. db.createCollection(<coll_name>) : create an empty collection with name given in parenthesis.
- db.createCollection('emp') creates a new collection named emp.
- db.emp.find() - works similar to select in RDBMS, find queries the table, as of now we don't have any document in EMP collection so nothing returned.
4. db.<coll>.insert({}):
- insert is use to insert documents into a mongo collection in JSON format or more specifically BSON format, in key-value pairs.
- WriteResult({ "nInserted" : 1 }) : 1 document inserted into collection
- _id: mongodb internally maintains unique id of every document similar to rowid in oracle.
5. db.<coll>.insertMany([{}]) :
- insert multiple documents into a collection.
6. db.<coll>.update(selection_criteria,update_date) :
- update documents in collections.
- insert two records with missing values and update values for the same.
- $set to update field values of documents fetched in selection criteria
- $in to search multiple values in a field similar to oracle in used in where clause
7. db.<coll>.update(selection_criteria,update_data) :
- updates only single document at a time, to update multiple documents, pass argument(multi=true)
- insert two documents with missing deptno values and then update the values.
- update statement resulted in updation of two documents.
8. db.remove(selection_criteria):
- deletes the records based on search criteria.
- remove documents having salary less than 5200
We have covered some basic operations and yes the basics. There is lot more to explore in MongoDB and will cover more concepts related to DB operations in coming blogs.
I hope I am able to hit the point and would be anxiously waiting for your response.
In case of any query kindly reach out to me @: vivekchaudhary.90@gmail.com
Thanks to Readers.
Comments
Post a Comment