 on Mongo Database Crude Operations. This is Dr. Anitha Pooja, Professor in Computer Science and Engineering Department at Valchen Institute of Technology, Solapur. At the end of this session, learners will be able to write queries for Crude Operations in MongoDB. Now let's start with the Crude Operations. Crude Operations stands for Create, Read, Update and Delete. Delete is to create a new collection in MongoDB. Read is to read the contents of the collection. Update is to update the documents or the fields in the documents in collection. Delete is to delete the documents in the collection. Now let's start with Create operation. The Create collection method can be used to create any new collection. For example, if you want to create a collection, say student profile in database studentDB. So here collection's name is student profile and this collection will be stored in which database it is studentDB. So I want to create a collection student profile in database studentDB. So first I will switch to that database. So I will use the command use studentDB. So I have switched to that database now. Then I will use db.createCollection in bracket the collection's name that is student profile. Immediately you will get the message OK1. That means the collection has been created. Now we move to the insert operation to insert new documents into the collection. So to insert operation is used to add new documents in the collection and to create a new collection if it does not exist. So even if you directly use insert operation to insert new documents into the collection, it will create a new collection if the collection does not exist. So let's see the example here. I want to insert one document into the student profile collection. So db.studentProfile.insert. Now the first field underscore ID is nothing but an object ID. That is it is an ID of that document. And this is unique for every document that is created in the collection. So by default it we can set its value to one. That is underscore ID I will start with the first document. So it has been given the value one. Then I have fields name, course and address. So name colon into quotes abc. Course is again a multi field here. So it consists of it's a composite data which consists of course name which is nothing but bcsc and duration four years. Again address is also composite data type. Like it consists of city, state and country. So city here is Bangalore, state is Ka and country is India. In this way I have created a new document in the collection student profile using insert operation. There is a scope for inserting multiple documents also in MongoDB. So I care for that the syntax is db.collectionname.insert.mini. This is used for inserting multiple documents into the collection. So let's take an example of a collection called as student list. Where in I want to insert three documents and each document has the fields roll number and name. So if the documents why if the insert operation does not specify an ID underscore ID field as it has been shown in the previous slide still it is okay. Okay the it MongoDB adds the underscore ID field with an object ID value to each document automatically by default. It will add the underscore ID field to every document. So here I have shown student db.studentlist.insert.mini. I have not taken the underscore ID field here at all. So MongoDB automatically adds it. Here I have directly started with the fields roll number and name. So I have added three documents into this collection. That is roll number one roll number name one name is Disha roll number two name is Eshu roll number three name is Swati. So all three documents have been inserted into collection student list. Now if I want to drop the collection then syntax is db. the name of the collection dot drop. Suppose I want to drop the student list then I have to use db.studentlist.drop. So in this way the complete collection is dropped or deleted from the database. Now we will start with the read operation. Now if I want to retrieve the document reading is nothing but retrieving the date of the document. So to retrieve the document the find method is used. So syntax is again db.collection name that is name of the collection dot find is the method used with that collection. So for example if I want to select all the documents from the student list collection then I need to use db.studentlist.find as it is shown here db.studentlist.find. So this selects all the documents from the collection student list and it displays on the terminal. If you want to format the result then I need to use one more method that is db.studentlist.find.pretty. This will display all the fields in a proper format that is it displays every field of the document on the new line. Now for example if I want to read a document from student list having name abc then I use db.studentlist.find which document I want to read that I specify here into the brackets. So name abc that will come into the curly brackets. So it will read only name of the student list collection having name as abc. Now if I want to read only name of the student list collection not their roll numbers then I have to use db.studentlist.find. So first field I will keep it as blank because I don't want to read that field at all. Then the second field I will start name 1 underscore id 0. In this way it will list the name of only the names of the student list collection. Now read operation on certain criteria. So I am reading the documents but based on certain conditions or criteria. So read documents having grade is set to c. So db.students is the name of the collection here.find what I am finding where the grade is equal to c. So just see here equal to is the operator used so dollar sign is used for the operators in MongoDB. So since here equal to is an operator dollar sign is used here and in single quotes its value is mentioned. So you read the documents whose grade is equal to c and plus you format that fields of the documents. Now read those documents from the student list where name begins with a. Now this condition is the name where the name begins with a I want to read only those documents. So db.studentlist.find where name now it begins with a. See there the cap symbol. This means the starting character is a. Give that character within the single forward slashes dot treaty. This is used again for formatting the document that is displayed. That is every field will be displayed on each new line of the document whose names begin with a. Next comes update operation. Update method is used to update documents in the collection. That is if you want to change the value of some fields if you want to delete some values or so on then we use update operation. So the method used here is update as shown here. So the syntax is db.collection name again dot update. Now update student list to modify the name where roll number is 10. So which document I want to modify where the roll number is 10. There I want to modify the name. So db.studentlist.update roll number 10. So first I am finding the I am using that condition. Then what I want to do I want to set a new value for it. So dollar set name is gargi. So I have changed the new name to gargi. Now to confirm this modification you can display the document. So db.studentlist.find. Now update can also be done using save operations. Save method is used to insert a new document. If the document with a specified id does not exist in the collection and it and it replaces it with a new one if it already exists. So save method it will insert a new document. If the document with a specified id does not exist in the collection and it replaces it with a new one if it is already existing. So syntax is db.collectionname.update. So update student list to modify name where roll number is 10. So this is how the syntax is. So dot save method is used here. To confirm this modification again you can use this query where it displays all the documents of the collection. Now think and write can update operation be used for adding a new field to the document. Update method is used to add a new field to the existing document. For example add a new field branch with the value csc to the document id3 of the student list collection. So I use update method here. I find the document with id3 and I set branch to csc. This new field is added here. To confirm this modification you can again use studentlist.find and that particular document only you can display in a particular format. Update else insert will modify document if it exists else it inserts it. So update student list to modify the name where roll number is 10. So that name is roll number 10 named gargi dollar set. In curly branch I have set branch csc and I set absurd true. Absurd what it will do is it will update this information into the collection. Okay if exists else it inserts it. If it exists it will modify it else it inserts this new document into the collection. Confirm this again you can use studentlist.find named gargi.trity. Similarly delete operation can be used to delete the documents in the collection db.collection name.remove method is used and field name I have set here as value. Just let's see deleted document from the student list where name is equal to gargi. So this condition will come here name is equal to gargi. So db.studentlist.remove roll number is 5. So I am deleting the document with the roll number 5 here or else you can use here the condition name is equal to gargi. So to confirm this deletion we can again use studentlist.find method name gargi which document I want to display is nothing but the gargi and .trity. These are some of the references that have been used for preparing this video. Thank you.