Skip to main content

Posts

Showing posts from March, 2016

Check Count, Delete Documents and Drop Collections in MongoDB

Checking the count of documents from collection in MongoDB: In RDBMS, to check the count from table, we need to use count function. In MongoDB also, we have count() method to get the count from collection. Syntax: db.<Collection Name>.count() Example: db.FirstCollection.count(). This query will return the count of total number of records. To get the count of records with specific filter then we need to use find() metho along with count(). Let's say we have records with different Student_Id values in StudentData collection. And we have to find the number of records with Student_Id 201. To do this, we have to pass below query. db.StudentData.find({"Student_Id":201}).count() Delete Documents in MongoDB: In RDBMS, to delete data from table, we need to use DELETE statements.As MongoDB is noSQL database, we have to use remove() method to delete data from collection. Syntax: db.<Collection Name>.remove({<data selection criteria>},{<

Updating Collection in MongoDB

We can use update() method to update a collection in MongoDB. Basic syntax will be like this: db.<Collection Name>.update(). To update() method, we need to pass the data selection criteria and what we need to update in the selected data. I have a below collection and data. I want to update student name who is having ID 100 in the above collection. To do this, I have to execute below query. We can upsert data as well into collection (Upsert definition: If data exists for the selection criteria then data will be updated otherwise, data will be inserted as new document into collection) To upsert data, we need to pass "upsert":true option to the update() method. In the below example, i am trying to upsert record with Student Id 102. But this 102 does not exist so it should insert as new record. update() will update only first record by default. To update multiple records, we need to pass "true" value for "multi" key in update query

Creating Collection And Inserting Data in MongoDB

Creating Collection in MongoDB:           We can create a collection in mongoDB using createCollecion(). This method will take collection name as parameter(collection name is the collection which you want to create). Syntax: db.createCollection('<Collection name>',options) Here, we can also pre-allocate space for new collection. This is optional. Let's assume, You have to create a collection with name "FirstCollection". You have to run below command. db.createCollection('FirstCollection') You can see the created collections using command "show collections". Inserting Documents: We can use insert() method to insert documents into a collection. If collection exists then document will be inserted. If not exists, it will create collection first and then it will insert document. Syntax: db.<collection Name>.insert() Below are the sample queries which will insert data into existing collection. Below are the