Devdit
 

Python MongoDB สร้างตารางข้อมูล

735

Python MongoDB สร้างตารางข้อมูล บทความนี้สอนเขียนภาษา Python ร่วมกับ MongoDB เพื่อสร้างตารางข้อมูล หรือ collection โดยการเพิ่มข้อมูลด้วยคำสั่ง insert_many พร้อมแสดงผลลัพธ์ออกสู่หน้าจอ สามารถเขียนโปรแกรมได้ดังนี้

 

ตัวอย่าง Python MongoDB สร้างตารางข้อมูล

from pymongo import MongoClient

conn = "mongodb://localhost:27017/"
client = MongoClient(conn)
db = client['example']
collection_name = db["book"]

item1 = {
    "name" : "Python & MongoDB",
    "price" : 350
}

collection_name.insert_many( [item1] )
print('Python MongoDB สร้างตารางข้อมูลสำเร็จ')

ผลลัพธ์

Python MongoDB สร้างตารางข้อมูลสำเร็จ

คำอธิบาย

Python MongoDB สร้างตารางข้อมูล จากตัวอย่างสร้างตาราง หรือ collection ชื่อ book พร้อมเพิ่มข้อมูลด้วย name = "Python & MongoDB" และ price = 350 ด้วยคำสั่ง insert_many ด้วย Python ร่วมกับ MongoDB

เขียน 1 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ