Last active
July 29, 2023 08:15
-
-
Save ahmaddidiks/af0b1653eeb30a266ea46e236cf0fac7 to your computer and use it in GitHub Desktop.
Flask and mongo DB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask import request | |
app = Flask(__name__) | |
@app.route('/',methods=[ 'GET']) | |
def hello_world(): | |
return 'GET METHOD' | |
@app.route('/data',methods=['POST']) | |
def data(): | |
input = request.args.get('sensor') | |
input2 = request.args.get('sensor2') | |
input3 = request.args.get('sensor3') | |
return f'sensor1= {input}, sensor2= {input2}, sensor3= {input3}' | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo # meng-import library pymongo yang sudah kita install | |
password = "mn8LS7xnBTMLNc39" | |
uri = f"mongodb+srv://ahmaddidiks:{password}@sic.mig4sp7.mongodb.net/?retryWrites=true&w=majority" | |
client = pymongo.MongoClient(uri) | |
db = client['182'] # ganti sesuai dengan nama database kalian | |
my_collections = db['sensor'] # ganti sesuai dengan nama collections kalian | |
# Data yang ingin dimasukkan | |
murid_1 = {'nama':'Didik','Jurusan':'IPA','Nilai':80} | |
murid_2 = {'nama':'Wilan', 'Jurusan':'TKJ','Nilai':90} | |
results = my_collections.insert_many([murid_1,murid_2]) | |
print(results.inserted_ids) # akan menghasilkan ID dari data yang kita masukkan |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo.mongo_client import MongoClient | |
password = "mn8LS7xnBTMLNc39" | |
uri = f"mongodb+srv://ahmaddidiks:{password}@sic.mig4sp7.mongodb.net/?retryWrites=true&w=majority" | |
# Create a new client and connect to the server | |
client = MongoClient(uri) | |
# Send a ping to confirm a successful connection | |
try: | |
client.admin.command('ping') | |
print("Pinged your deployment. You successfully connected to MongoDB!") | |
except Exception as e: | |
print(e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo # meng-import library pymongo yang sudah kita install | |
client = pymongo.MongoClient("MASUKAN ID KALIAN") | |
password = "mn8LS7xnBTMLNc39" | |
uri = f"mongodb+srv://ahmaddidiks:{password}@sic.mig4sp7.mongodb.net/?retryWrites=true&w=majority" | |
client = pymongo.MongoClient(uri) | |
db = client['gudang'] # ganti sesuai dengan nama database kalian | |
my_collections = db['rak'] # ganti sesuai dengan nama collections kalian | |
for x in my_collections.find(): | |
print(x["nama"], x["Nilai"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment