[100 CYCLE SPECIAL] How to use MongoDB PYTHON
CoolJames1610 (665)

Hey guys and welcome to this tutorial

bruh why am i doing it like a youtube video XD

Before we start, thanks to all who have upvoted anything of mine (projects, comments etc). Go upvote the below if you already haven't XD


First, go to mongodb.com

and create an account!

Then, after you have created your account, it will show you three plans. I would recommend getting the FREE one for testing purposes and it means you don't have to pay

After that, you select which provider you want and where that server-cloud thingy is. The star is used for recommended ones. Then name your cluster!

IG MEANS IGNORE Because if you change them, it changes to a paid plan :(

Then, MongoDB will start making your cluster. It takes a few minutes. So while you are waiting you can play PyTube or something XD

Once it has finished, it should look like this. Then you press the connect button

It will then ask you for some info. At first, use your own IP and create a user. You will need to remember your password!

Once you have filled in the details, it is TIME TO CONNECT WITH PYTHON - (there are lots of other languages you can use but I only know python...)

Select connect your application

Since repl uses the latest version of every language (i think), choose python and go to 3.6 or later

Click show full driver and copy the contents! You can now press close... IT IS TIME FOR PYTHON!

Oh and before you go to python, go to Network Access, add a new IP ADDRESS and allow ACCESS FROM ANYWHERE, then wait until MongoDB has added that :) :D


We need to import pymongo and dns

import pymongo, dns 

Then, paste your connection string that you pasted from MongoDB

This is mine -->

client = pymongo.MongoClient("mongodb+srv://Example:[email protected]/test?retryWrites=true&w=majority") 

To create a database, you can use either:

db = client.Example


db = client["Example"]

<-- Use this way if you want the user to be able to create their own database with their own name (look in project below for example)

And it is exactly the same for the collection!

 mycol = db.Example


 mycol = db["Example"]

<-- Again use this way for the example stated above ;)

To add documents to your database, use a dictionary inside a list:

 documents = [{"Name": "CoolJames", "Upvote?": True, "Cycles?": 100}]

MongoDB automatically knows that "Name" contains a string, "Upvote? contains a boolean and that "Cycles?" contains an integer!

Then do:



If you have done it right, it should look something like this:

But what if I want to get my data from my database?

To get your data, use the connection string and assign db and mycol to the same name in the database. (In my case, db = client.CoolJames and mycol = db.CoolJames)

You can still use client[name] and db[name]. Look in project for example but they have to equal what the database and collection are named in MongoDB

To collect the data, we use .find

You can either find one value or find many

To find many, you would loop each time and do the converting and to find one, you would only need to do it once. Look in project for more detail.

I would use loop once if I'm trying to get user info that is in a separate database (SEE PYTUBE)

I would use loop until... if I want to get all the data from a collection (SEE GLOBAL REPL COUNTER)

Now the next is how to assign variables to the data that you have collected. This took me a few days to find out (by going on several websites and piecing the code together)

Now we want x in a string so we can do x = "'" str(x) + "'" and that would enclose x in a string.

We then import re and use .sub to remove the braces and any other special characters that we don't want.

We then enclose it in a list

lst = [x]

and use the function convert (d = convert(lst) -->

def convert(lst): 
return ' '.join(lst).split()

This puts each word as a separate entity in a list and then you can easily assign each variable to an index in the list e.g.

name = d[1]
upvote = d[3]
cycles = d[5]

And there you have it! You have exported data to the database, then collected it back and assigned the data into variables that you can use! This is why you are able to get your data easily in PyTube and use them!

This has taken SO long to write out
an upvote would be very helpful XD
ahhh im so tired now

<3 MongoDB

Project below helps a bit more!

SHOUTOUT TO: @Zuhdi28, @NoelBryan

BTW @amasad commented :O :O :O

There are loads more things you can do with MongoDB, click here for more information!

Thanks for reading!


You are viewing a single comment. View All
JordanDixon1 (451)

@Warhawk947 That would be me XD but yeah that would work for most of the programmers out there.