M101P: MongoDB for developers
All resources I am using for the M101P course by MongoDB University
Content
Week 1
- hello_bottle.py
A sample file to start running the Bottle framework
python hello_bottle.py
- hello_bottle.py
A sample file to connect a python file with Mongo
To be able to execute this, you just need to do:<br/><br/> Execute this command in your terminal (This will start MongoDB in your machine, you need to install MongoDB first to be able to go through this): <br/> <code>SekthDroid-Mac:week1 SekthDroid$ mongod</code> <br/> <br/> Execute this one to execute the python file <br/> <code>SekthDroid-Mac:week1 SekthDroid$ python hello_pymongo.py</code> </p> </li> <li>hello_app.py <p>A file used like a glue for Bottle Framework and MongoDB. It can be executed and tested using a browser, in this case, using the url "http://localhost:8082/"<br/> <code>SekthDroid-Mac:week1 SekthDroid$ mongod</code> <br/> <code>SekthDroid-Mac:week1 SekthDroid$ python hello_app.py</code> </p> </li> <li>bottle_url_handler.py <p>A file used to setup some handlers for the urls, as we can see, there are 2 defined urls, one for the root web, which will pring <code>Hello World</code> and another one that will be executed when we access to the url http://localhost:8080/test_page and we will see the text <code>This is a test page</code></p> </li> <li>hello_bottle_things.py <p>A file used to setup templates with bottle framework, and pass it some values that will be rendered with the template processor. The content of the template is located in /week1/hello_world.tpl.</p> </li> <li>hello_bottle_things_2.py <p>This is a file that will get the data from a form request, and will render another template with the data the user wrote in the form</p> </li>
Week 2
In order to be able to work with the exercises, we will need to run the file within the folder seeder, which is a common js file that will seed a database
with some dummy data. Just execute it in a shell like this:
SekthDroid-Mac:Week2 SekthDroid$ mongo < create_student_collection.js
This will fill a db called school with 300 dummy data.
- using_find.py
This is a simple file that will execute a different kinds of queries to the database and then it will be printed.
python using_find.py
- find_with_regex.py
This is a file that uses a regex expression to return results from our reddit database that we have imported using the seeder (located at seeder/reddit_seeder.py)
python find_with_regex.py
- find_sort_skip_limit.py
This is a file that uses the Sort, Skip and Limit features in the find method of Pymongo. It works with the school database (The one we created with the create_student seeder)
python find_sort_skip_limit.py
- using_insert_one.py
This is a file that will insert 2 persons in the collection "people" of the database "school". Due to one of them have an
"_id"
, if you run this script more than one time, the one who hasn't got the"_id"
will be persisted again, the the other one containing this key will throw an exception. It's an example on how theinsert_one
works, and how the Mongo id generation runs.
python using_insert_one.py
python using_insert_one.py
(Will throw an error inserting the second one) - using_insert_many.py
This is a file that will show the behaviour of the
insert_many
function of the Pymongo driver. As you can see in the sample, one of the items to insert contains a key called"_id"
. As we saw in the previous sample, this item will not be persisted again, and will throw an Error. But if we see, we can send a boolean that will perform the bulk insert in order if isTrue
, or not in order if isFalse
, so if we execute the program with the boolean set toTrue
, it will insert Android multiple times, and the error will be thrown at the second element, so the rest of the items will be not persisted, even if some item doesn't have any"_id"
key. By other hand, if we run this with the flag set toFalse
, it will insert the elements until it's the turn of the item with the"_id"
key, where the error will be thrown.
python using_insert_many.py
(Errors will be thrown when the secondinsert_many
is called, but some elements could be possible inserted, due to theFalse
flag.)
python using_insert_many.py
(Android will be inserted in the firstinsert_many
always, but not iOS or Windows)
- using_update_many.py
This file contains some logic about the
update_one
andupdate_many
functions of PyMongo. We will see how we can update just one document, or update all. We make use of the$set
to set a value in the document we want (or all if we usefind_all
python using_update.py
- update_using_replace.py
This file shows the use of the
replace_one()
function of PyMongo, where first we will clear all thereview_date
values for all the rows, and then we will find a student, add a review date, and replace the entire student with the new value. Then we will find it again and printing the result to see what has changed.
python update_using_replace.py
- using_upsert.py
This file show the use of the
upsert
flag when usingupdate_one
,update_many
andreplace_one
, and some use case where it can fails
python using_upsert.py
- using_delete.py
This file contains the basic usage of the
delete_one
anddelete_many
functions. As we can see, if we usedelete_one
it will delete only one element, so we can see that there are still 2 more elements for a student, but if we calldelete_many
we can see how all the rest of the elements are deleted
python using_delete.py
- using_find_and_modify.py
This file contains a sample of the
find_one_and_update
function of the Pymongo driver. With this we can retrieve the value that are being updated before or after the execution, so we can do things with it
python using_find_and_modify.py