MongoDB and PyMongo

Before you head into the text, do watch the video tutorial:

Here are some of the important PyMongo functions:


  1. insert_one({}):

    This is used to enter one post into the collection. The dictionary in the brackets will contain the entry.

  2. insert_many([{}, {}, ....]):

    This is used to enter multiple posts at once and accepts a list of dictionaries as the argument.

  3. find_one({}):

    This is used to find one post in the collection. It accepts a dictionary containing the condition by which to search as argument.
    Eg:

    collection.find_one({'_id': 1})

    The above statement will return the post whose id is 1.

    Note that if there are multiple posts in the collection satisfying the condition provided by you, ONLY 1 will be fetched.

  4. find({}):

    This function is used to fetch all the records satisfying the condition in the dictionary that is passed as argument. The records fetched will be encoded and you will have to parse through them using a loop to print them one by one.
    Eg:

    records = collection.find('City': 'Mumbai')
    for record in records:
    print(record)

    The above code snippet will fetch all the records from the collection where the 'City' field is set as 'Mumbai'. The for loop will then parse through the fetched records and print each one as a dictionary.

    Note that if you pass an empty dictionary as the argument, the program will fetch ALL the records.

  5. count_documents({}):

    This function is used to count the number of posts satisfying the condition(s) passed as argument in the dictionary. It returns an int data type.

    Note that if you simply pass an empty dictionary as argument, the program will count total number of posts in the collection and return that.

  6. update_one({<condition>}: {'$set': {<updated field>}}):

    Now, admittedly, the syntax for this one looks a little complex and there's a lot to unpack but it's actually pretty simple once you understand it.

    The first dictionary will have the criteria by which to find the record that has to be updated. The second dictionary is telling the program to 'set' certain field(s) to something else. The final list within the second list will contain the field to be updated and the new value for it.

    collection.update_one({'_id': 2}, {'$set': {'Salary': 1500000.00}})

    The above statement will update the record that has id 2. It will set the Salary field to 1500000.

    Similar to find_one, update_one will also update only the first record satisfying the condition provided even if there are multiple records satisfying the condition.

    You may also use
    '$inc' in place of '$set' to increase the value of a field by some amount.

  7. update_many({<condition>}: {'$set': {<updated field>}}):

    The arguments work the same way as for update_one but it will update all the records in the collection that satisfy the condition provided by you.

That's all the basics. If you have any questions, do leave them in the YouTube comments section or drop us an email.

Also, check out the official documentation for PyMongo here: Official Documentation for PyMongo

That's all for today. Cya!