<jupyter_start><jupyter_text>Import the pymongo LibraryThe first step in using `pymongo` is to import it into our local context.This exposes all methods exported from the `pymongo` library and makes them available in our local context.For additional information on all methods available in `pymongo`, visit the [documentation](https://api.mongodb.com/python/current/)<jupyter_code>import pymongo<jupyter_output><empty_output><jupyter_text>Establish a connectionWhen working with `pymongo` is to create a client that exposes the connection to your MongoDB instance. Since we're working with MongoDB Atlas, you'll need the connection URI - you can obtain this by clicking the _CONNECT_ button from within the Atlas Management Console. ![Connect](atlas_connect.gif)   Create a variable to hold the URI String which points to our MongoDB Atlas Cluster. Note the use of the SRV Record. This simplifies reference to a cluster of nodes.<jupyter_code>uri = "mongodb+srv://mike:Password<PASSWORD>@mflix-zbcul.mongodb.net/test?retryWrites=true&w=majority"<jupyter_output><empty_output><jupyter_text>Now, create a client object that will expose methods from our pymongo MongoClient connection.<jupyter_code>client = pymongo.MongoClient(uri)<jupyter_output><empty_output><jupyter_text>Create a variable that points to the database from our client connection.In this case, we\'re accessing our `sample_mflix` collection in our cluster. If you have not loaded the sample databases, review this document.<jupyter_code>mflix = client.sample_mflix<jupyter_output><empty_output><jupyter_text>Create a variable pointing to the collection within our mflix database<jupyter_code>movies = mflix.movies<jupyter_output><empty_output><jupyter_text>CREATE: Creating, or Inserting Data into MongoDBNow that we have a connection, let's Create a new document in the movies connection[Documentation for Creating](https://docs.mongodb.com/manual/crud/create-operations)  Inserting a Single Document<jupyter_code>movies.insert_one({
    'title': 'a new movie',
    'fullplot': 'a new movie about a panda that eats shoots and leaves',
    'countries': ['USA'],
     'year': 1893,
     'type': 'movie',
     'genres': ['Short'],
     'cast': ['Panda', '<NAME>']
})<jupyter_output><empty_output><jupyter_text>Inserting Many DocumentsNote that you can simply create an array of JSON documents and pass that to the insert_many method.<jupyter_code>moremovies = [
    {
        "title": "a title",
        'fullplot': 'a movie',
        'countries': ['USA'],
        'year': 1891,
         'type': 'movie',
         'genres': ['Short'],
         'cast': ['<NAME>', '<NAME>']
    },{
        "title": "b title",
        'fullplot': 'b movie',
        'countries': ['USA'],
        'year': 1894,
         'type': 'movie',
         'genres': ['Short'],
         'cast': ['Getty Lee', '<NAME>']
    },{
        "title": "c title",
        'fullplot': 'c movie',
        'countries': ['USA'],
        'year': 1892,
         'type': 'movie',
         'genres': ['Short'],
         'cast': ['Sasha Baron Cohen']
    }
]
movies.insert_many(moremovies)<jupyter_output><empty_output><jupyter_text>Reading Documents with Pymongo[Documentation for Reads](https://docs.mongodb.com/manual/crud/read-operations)With relational, or _tabular_ databases, to "select" all "rows" in a "table", we would:```SELECT * FROM movies```The terminology differs slightly - but the elements in data persistence are quite similar. Database Elements|Tabular|Document||-------|-------||table|collection||row|document||column|key||index|index| Data Query Commands|SQL|MQL||---|---||select|find|With that in mind, To "find" all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:<jupyter_code>import pprint
cursor = movies.find({}).limit(10)
for document in cursor:
    pprint.pprint(document) # iterate the cursor
movies.find_one({'title': 'a title'})
import pprint
pprint.pprint(movies.count_documents({"cast": "Salma Hayek"}))
pprint.pprint(movies.count_documents({"title": "a title"}))<jupyter_output>21
6<jupyter_text>Update our movie document[Documentation for Updates](https://docs.mongodb.com/manual/tutorial/update-documents/)<jupyter_code>find_query = { "title": "a title"}
new_value = { "$set": { "writers": [ "<NAME>", "<NAME>", "<NAME>"] } }

movies.update_one(find_query, new_value)

movies.find_one({"title": "a new movie"})<jupyter_output><empty_output><jupyter_text>Deleting Documents[Documentation for Deletes](https://docs.mongodb.com/manual/tutorial/remove-documents/) Deleting one at a time<jupyter_code>movies.delete_one({"title": "a title"})
# find it?
movies.find_one({"title": "a title"})<jupyter_output><empty_output><jupyter_text>Deleting many<jupyter_code>myquery = { "title": {"$regex": "^[abc] title"} }
x = movies.delete_many(myquery)
print(x.deleted_count, " movies deleted.")<jupyter_output>12  movies deleted.
{'fraction_non_alphanumeric': 0.13255673830086362, 'fraction_numerical': 0.004619401486242217, 'mean_word_length': 5.110429447852761, 'pattern_counts': {'":': 13, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '57638', 'license': 'MIT', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1521, 'n_words': 444, 'path': 'M220P - MongoDB for Python Developers/Files/notebooks/Create_Read_Update_Delete.ipynb', 'repo_name': 'ReynerGonzalez/mongodb-university-course', 'size': '5016'}