Common Operations in MongoDb

Common Operations in MongoDb

Some Common Operations in MongoDB

  1. In MongoDb first, we need to create a connection to the MongoDB client, then create a database then create a collection that will store the documents

    ```python import pymongo ## importing the python library for mongodb connection dbconnection=pymongo.MongoClient("mongodb://localhost:27017")

    on printing db connection we get the output as

## ['admin', 'config', 'local'] ##
##MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True['admin', 'config', 'local'] ##
### MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True ##
dbname='inueorn' ## now we are creating a database
db=dbconnection[dbname]
print(db)## on printing the database we get 
## Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'inueorn') 
dbconnection.list_database_names()
## will return a list of all the database 
## now we need to create a collection 
collection_name='jeevan'
collection=db[collection_name]
print(collection)
## on printing the collection we get 
## Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'inueorn'), 'jeevan')
```
  1. Inserting document into our collection

    Note : It should be enclosed inside

    {
    # this is a must
    
    }
    

    Some commands to insert elements are

    my_row = {'Serial No': '9998',
     'GRE Score': '337',
     'TOEFL Score': 'a118',
     'University Rating': '4',
     'SOP': '4.5',
     'LOR': '4.5',
     'CGPA': '9.65',
     'Research': '1',
     'Chance of Admit': '0.92'}
    
    index= collection.insert_one(my_row)
    print(index)
    ## on printing it should gives us the object id which is unique
    ## <pymongo.results.InsertOneResult object at 0x7f0b5ac06250>
    ## for inserting multiple document we give it like an array of objects
    y_rows_1= [
    {'Serial No': '9997',
     'GRE Score': '337',
     'TOEFL Score': '118',
     'University Rating': '4',
     'SOP': '4.5',
     'LOR': '4.5',
     'CGPA': '9.65',
     'Research': '1',
     'Chance of Admit': '0.92'},
      {
        'Serial No': '9996',
     'GRE Score': '336',
     'TOEFL Score': '118',
     'University Rating': '4',
     'SOP': '4.5',
     'LOR': '4.5',
     'CGPA': '9.6',
     'Research': '0',
     'Chance of Admit': '0.92'},
      {
        'Serial No': '9995',
     'GRE Score': '340',
     'TOEFL Score': '118',
     'University Rating': '4',
     'SOP': '4.5',
     'LOR': '4.5',
     'CGPA': '9.5',
     'Research': '1',
     'Chance of Admit': '0.92'},
      {
        'Serial No': '9994',
     'GRE Score': '334',
     'TOEFL Score': '119',
     'University Rating': '4',
     'SOP': '4.5',
     'LOR': '4.5',
     'CGPA': '9.4',
     'Research': '1',
     'Chance of Admit': '0.92'},
      {
        'Serial No': '9993',
     'GRE Score': '317',
     'TOEFL Score': '118',
     'University Rating': '4',
     'SOP': '4.5',
     'LOR': '4.5',
     'CGPA': '9.65',
     'Research': '1',
     'Chance of Admit': '0.92'}
    
    ]
    collection.insert_many(my_rows_1)
    ## on printing this we get the id 
    ## <pymongo.results.InsertManyResult at 0x7f0b3ffe3370>
    
  2. Iterating through the elements

    ```python result=collection.find() for i in result:

    print(i)
    

    output is

{'_id': ObjectId('63a339968a632865286c5819'), 'Serial No': '9998', 'GRE Score': '337', 'TOEFL Score': 'a118', 'University Rating': '4', 'SOP': '4.5', 'LOR': '4.5', 'CGPA': '9.65', 'Research': '1', 'Chance of Admit': '0.92'}
{'_id': ObjectId('63a33a358a632865286c581a'), 'Serial No': '9997', 'GRE Score': '337', 'TOEFL Score': '118', 'University Rating': '4', 'SOP': '4.5', 'LOR': '4.5', 'CGPA': '9.65', 'Research': '1', 'Chance of Admit': '0.92'}
{'_id': ObjectId('63a33a358a632865286c581b'), 'Serial No': '9996', 'GRE Score': '336', 'TOEFL Score': '118', 'University Rating': '4', 'SOP': '4.5', 'LOR': '4.5', 'CGPA': '9.6', 'Research': '0', 'Chance of Admit': '0.92'}
{'_id': ObjectId('63a33a358a632865286c581c'), 'Serial No': '9995', 'GRE Score': '340', 'TOEFL Score': '118', 'University Rating': '4', 'SOP': '4.5', 'LOR': '4.5', 'CGPA': '9.5', 'Research': '1', 'Chance of Admit': '0.92'}
{'_id': ObjectId('63a33a358a632865286c581d'), 'Serial No': '9994', 'GRE Score': '334', 'TOEFL Score': '119', 'University Rating': '4', 'SOP': '4.5', 'LOR': '4.5', 'CGPA': '9.4', 'Research': '1', 'Chance of Admit': '0.92'}
{'_id': ObjectId('63a33a358a632865286c581e'), 'Serial No': '9993', 'GRE Score': '317', 'TOEFL Score': '118', 'University Rating': '4', 'SOP': '4.5', 'LOR': '4.5', 'CGPA': '9.65', 'Research': '1', 'Chance of Admit': '0.92'} ###
result=collection.find({}).limit(3) ## limiting the number of records
```

4.  Deleting & Updating new entries

    ```python
    my_query_3={"GRE Score":"340"}

    #how to delete record
    x=collection.delete_one(my_query_3)

    x.deleted_count
    #how to update record in mongodb
    my_record={"GRE Score":"350"}
    new_value={"$set":{"GRE Score":"345"} }

    #update many
    y=collection.update_many(my_record,new_value)
    y.modified_count

    y.raw_result

    #update one
    z=collection.update_one(my_record,new_value)
    ```