บทช่วยสอนงูหลาม

Python HOME แนะนำ Python Python เริ่มต้น ไวยากรณ์หลาม ความคิดเห็นหลาม ตัวแปรหลาม ประเภทข้อมูลหลาม ตัวเลขหลาม Python Casting Python Strings Python Booleans ตัวดำเนินการ Python รายการหลาม Python Tuples ชุดหลาม พจนานุกรม Python Python If...Else Python ในขณะที่ลูป Python สำหรับลูป ฟังก์ชั่นหลาม Python Lambda Python Arrays Python คลาส/วัตถุ มรดกหลาม Python Iterators ขอบเขตหลาม โมดูล Python วันที่หลาม คณิตศาสตร์หลาม Python JSON Python RegEx Python PIP Python ลอง...ยกเว้น อินพุตผู้ใช้ Python การจัดรูปแบบสตริงหลาม

การจัดการไฟล์

การจัดการไฟล์ Python Python อ่านไฟล์ Python เขียน/สร้างไฟล์ Python ลบไฟล์

โมดูล Python

NumPy กวดวิชา เกมส์หมีแพนด้า กวดวิชา Scipy

Python Matplotlib

บทนำ Matplotlib Matplotlib เริ่มต้น Matplotlib Pyplot Matplotlib พล็อต เครื่องหมาย Matplotlib Matplotlib Line ป้ายกำกับ Matplotlib Matplotlib Grid แผนย่อย Matplotlib Matplotlib Scatter Matplotlib บาร์ Matplotlib Histograms Matplotlib แผนภูมิวงกลม

การเรียนรู้ของเครื่อง

เริ่มต้น โหมดค่ามัธยฐาน ส่วนเบี่ยงเบนมาตรฐาน เปอร์เซ็นไทล์ การกระจายข้อมูล การกระจายข้อมูลปกติ พล็อตกระจาย การถดถอยเชิงเส้น การถดถอยพหุนาม การถดถอยพหุคูณ มาตราส่วน รถไฟ/ทดสอบ ต้นไม้การตัดสินใจ

Python MySQL

MySQL เริ่มต้น MySQL สร้างฐานข้อมูล MySQL สร้างตาราง MySQL Insert MySQL Select MySQL Where MySQL สั่งซื้อโดย MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL เข้าร่วม

Python MongoDB

MongoDB เริ่มต้น MongoDB สร้างฐานข้อมูล MongoDB สร้างคอลเล็กชัน MongoDB แทรก MongoDB ค้นหา แบบสอบถาม MongoDB MongoDB Sort MongoDB ลบ MongoDB Drop Collection อัพเดท MongoDB MongoDB Limit

การอ้างอิงหลาม

ภาพรวมของ Python ฟังก์ชันในตัวของ Python Python String Methods Python List Methods วิธีพจนานุกรม Python วิธี Python Tuple Python Set Methods วิธีไฟล์ Python คีย์เวิร์ด Python ข้อยกเว้นของ Python คำศัพท์หลาม

การอ้างอิงโมดูล

โมดูลสุ่ม โมดูลคำขอ โมดูลสถิติ โมดูลคณิตศาสตร์ โมดูล cMath

Python ฮาวทู

ลบรายการที่ซ้ำกัน ย้อนกลับสตริง เพิ่มสองตัวเลข

ตัวอย่าง Python

ตัวอย่าง Python Python Compiler แบบฝึกหัดหลาม แบบทดสอบ Python ใบรับรอง Python

Python MongoDB แทรกเอกสาร


เอกสารใน MongoDB เหมือนกับบันทึก ในฐาน ข้อมูล SQL

แทรกลงในคอลเล็กชัน

ในการแทรกบันทึกหรือเอกสารตามที่เรียกว่า MongoDB ลงในคอลเล็กชัน เราใช้ insert_one()เมธอด

พารามิเตอร์แรกของinsert_one()เมธอดคือพจนานุกรมที่มีชื่อและค่าของแต่ละฟิลด์ในเอกสารที่คุณต้องการแทรก

ตัวอย่าง

แทรกบันทึกในคอลเลกชัน "ลูกค้า":

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mydict = { "name": "John", "address": "Highway 37" }

x = mycol.insert_one(mydict)

ส่งคืนฟิลด์ _id

เมธอดส่ง คืนinsert_one()อ็อบเจ็กต์ InsertOneResult ซึ่งมีคุณสมบัติinserted_idที่เก็บ id ของเอกสารที่แทรก

ตัวอย่าง

แทรกระเบียนอื่นในคอลเลกชัน "ลูกค้า" และส่งกลับค่าของ _idฟิลด์:

mydict = { "name": "Peter", "address": "Lowstreet 27" }

x = mycol.insert_one(mydict)

print(x.inserted_id)

หากคุณไม่ระบุ_idฟิลด์ MongoDB จะเพิ่มหนึ่งฟิลด์ให้คุณและกำหนดรหัสเฉพาะสำหรับแต่ละเอกสาร

ในตัวอย่างด้านบนไม่_idได้ระบุฟิลด์ ดังนั้น MongoDB จึงกำหนด _id เฉพาะสำหรับเร็กคอร์ด (เอกสาร)



แทรกหลายเอกสาร

ในการแทรกหลายเอกสารลงในคอลเลกชันใน MongoDB เราใช้ insert_many()วิธีการ

พารามิเตอร์แรกของinsert_many()วิธีการคือรายการที่มีพจนานุกรมพร้อมข้อมูลที่คุณต้องการแทรก:

ตัวอย่าง

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mylist = [
  { "name": "Amy", "address": "Apple st 652"},
  { "name": "Hannah", "address": "Mountain 21"},
  { "name": "Michael", "address": "Valley 345"},
  { "name": "Sandy", "address": "Ocean blvd 2"},
  { "name": "Betty", "address": "Green Grass 1"},
  { "name": "Richard", "address": "Sky st 331"},
  { "name": "Susan", "address": "One way 98"},
  { "name": "Vicky", "address": "Yellow Garden 2"},
  { "name": "Ben", "address": "Park Lane 38"},
  { "name": "William", "address": "Central st 954"},
  { "name": "Chuck", "address": "Main Road 989"},
  { "name": "Viola", "address": "Sideway 1633"}
]

x = mycol.insert_many(mylist)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)

เมธอดส่ง คืนinsert_many()อ็อบเจ็กต์ InsertManyResult ซึ่งมีคุณสมบัติinserted_idsที่เก็บรหัสของเอกสารที่แทรก


แทรกเอกสารหลายฉบับพร้อมรหัสที่ระบุ

หากคุณไม่ต้องการให้ MongoDB กำหนดรหัสเฉพาะสำหรับเอกสารของคุณ คุณสามารถระบุฟิลด์ _id เมื่อคุณแทรกเอกสาร

จำไว้ว่าค่าจะต้องไม่ซ้ำกัน เอกสารสองฉบับไม่สามารถมี _id เดียวกันได้

ตัวอย่าง

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mylist = [
  { "_id": 1, "name": "John", "address": "Highway 37"},
  { "_id": 2, "name": "Peter", "address": "Lowstreet 27"},
  { "_id": 3, "name": "Amy", "address": "Apple st 652"},
  { "_id": 4, "name": "Hannah", "address": "Mountain 21"},
  { "_id": 5, "name": "Michael", "address": "Valley 345"},
  { "_id": 6, "name": "Sandy", "address": "Ocean blvd 2"},
  { "_id": 7, "name": "Betty", "address": "Green Grass 1"},
  { "_id": 8, "name": "Richard", "address": "Sky st 331"},
  { "_id": 9, "name": "Susan", "address": "One way 98"},
  { "_id": 10, "name": "Vicky", "address": "Yellow Garden 2"},
  { "_id": 11, "name": "Ben", "address": "Park Lane 38"},
  { "_id": 12, "name": "William", "address": "Central st 954"},
  { "_id": 13, "name": "Chuck", "address": "Main Road 989"},
  { "_id": 14, "name": "Viola", "address": "Sideway 1633"}
]

x = mycol.insert_many(mylist)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)