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 PyTube GLOBAL REPL COUNTER
WELCOME TO THE MONGODB TUTORIAL!
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
NOW IT IS TIME FOR PYTHON XD
We need to import pymongo and dns
import pymongo, dns
Then, paste your connection string that you pasted from MongoDB
MongoDB automatically knows that "Name" contains a string, "Upvote? contains a boolean and that "Cycles?" contains an integer!
Then do:
mycol.insert_many(documents)
CONGRATS! YOU HAVE SUCCESSFULLY EXPORTED DATA INTO MONGODB
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!
Woweeee This has taken SO long to write out an upvote would be very helpful XD ahhh im so tired now
One slight problem... The database password is open for all to see!!! If only there was a way to encrypt it but be able to have anyone use the application....
But it's obviously not the best way to do it and when people FORK the project, then they kinda have complete control on what gets added to that database. I'll try and experiment and find out if there is any solution! @JordanDixon1
@CoolJames1610 At first my mind went to Bcrypt. Unfortunately though, we have to test the password against a string so the password is still visible...
@JordanDixon1 usually, if you put the password in a different file and name it something unsuspicious, like gameUtils.py, and put secret=fsjlfsfjisdjfljfklsjflisedjflsjef<insert token thing here>ineedhelpwithmylife, and in your main.py file type from gameUtils.py import secret, no one will notice anything, unless they are like a chad programmer and like to read every single file that you have in your repl
@Warhawk947 lol, that's hilarious. I don't feel too safe doing that though. Maybe if we also encoded it in base64 maybe?? And then we could compile using the bash/python command:
@Warhawk947 Yep. I don't think you can do it in Repl.it but I know you can do it on windows, Linux, or mac (with python of course.) Repl.it doesn't allow you to look at home/runner files or something like that soooo.
@Warhawk947 heres the link on how to decode base64 after decoding it: https://repl.it/@JordanDixon1/PythonB64Pass make sure you go to main.py instead of main.sh. After you understand the way base64 is decoded you could use an encoder and encode the password then you could compile the python file which then you could import as a module.
[100 CYCLE SPECIAL] How to use MongoDB PYTHON
Hey guys and welcome to this tutorial
bruh why am i doing it like a youtube video XDBefore we start, thanks to all who have upvoted anything of mine (projects, comments etc). Go upvote the below if you already haven't XD
PyTube
GLOBAL REPL COUNTER
WELCOME TO THE MONGODB TUTORIAL!
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 payAfter that, you select which provider you want and where that server-cloud
thingyis. 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 XDOnce 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
onlyknow 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
NOW IT IS TIME FOR PYTHON XD
We need to import pymongo and dns
Then, paste your connection string that you pasted from MongoDB
This is mine -->
To create a database, you can use either:
or:
<-- 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!
or:
<-- Again use this way for the example stated above ;)
To add documents to your database, use a dictionary inside a list:
MongoDB automatically knows that
"Name"
contains a string,"Upvote?
contains a boolean and that"Cycles?"
contains an integer!Then do:
CONGRATS! YOU HAVE SUCCESSFULLY EXPORTED DATA INTO MONGODB
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
andmycol
to the same name in the database. (In my case,db = client.CoolJames
andmycol = db.CoolJames
)You can still use
client[name]
anddb[name]
. Look in project for example but they have to equal what the database and collection are named in MongoDBTo 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 itonce
. 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 dox = "'" 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
and use the function
convert
(d = convert(lst)
-->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.
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!
Woweeee
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!
@CoolJames1610
One slight problem... The database password is open for all to see!!! If only there was a way to encrypt it but be able to have anyone use the application....
Yeah, that's a problem I had too. I put the password in an .env file but it meant that no one could use it. This is what I do now:
But it's obviously not the best way to do it and when people FORK the project, then they kinda have complete control on what gets added to that database. I'll try and experiment and find out if there is any solution!
@JordanDixon1
@CoolJames1610 At first my mind went to Bcrypt. Unfortunately though, we have to test the password against a string so the password is still visible...
yeah lol sigh @JordanDixon1
@JordanDixon1 usually, if you put the password in a different file and name it something unsuspicious, like gameUtils.py, and put
secret=fsjlfsfjisdjfljfklsjflisedjflsjef<insert token thing here>ineedhelpwithmylife
, and in your main.py file typefrom gameUtils.py import secret
, no one will notice anything, unless they are like a chad programmer and like to read every single file that you have in your repl@Warhawk947 That would be me XD but yeah that would work for most of the programmers out there.
Okay thanks! @Warhawk947
@JordanDixon1 yeah, i did that once, and it even though it was pretty popular
shameless plugno one messed up the database so i guess it works?@Warhawk947 lol, that's hilarious. I don't feel too safe doing that though. Maybe if we also encoded it in base64 maybe?? And then we could compile using the bash/python command:
@JordanDixon1 you can do that? I never knew!
@Warhawk947 Yep. I don't think you can do it in Repl.it but I know you can do it on windows, Linux, or mac (with python of course.) Repl.it doesn't allow you to look at home/runner files or something like that soooo.
@Warhawk947 heres the link on how to decode base64 after decoding it: https://repl.it/@JordanDixon1/PythonB64Pass make sure you go to main.py instead of main.sh. After you understand the way base64 is decoded you could use an encoder and encode the password then you could compile the python file which then you could import as a module.
or
And there ya go.
Ooh thanks! @JordanDixon1
@JordanDixon1 cool!
@Warhawk947 I took your advice and put it in MongoDBUtils.py here https://repl.it/@NoelBryan/MongoDB and then I imported it with like 10 other things so nobody noticies
Your code works perfectly! Well done! :D And wow - for ur connection string XD @NoelBryan
Thank you! Still, somebody could look through the PyMongoUtls but it works! @CoolJames1610
@NoelBryan If you have python on your computer you could also use base64 encoding, decoding and then compile the file and import it then.
Yeah lol @NoelBryan
I’ll try that out! @JordanDixon1
@NoelBryan lol nice
@JordanDixon1 env
Env wont work if u want people to be able to connect to the database @AmazingMech2418
@CoolJames1610 Store the link/password in the env file...
ok @AmazingMech2418