การจัดเก็บข้อมูลในฐานข้อมูล NoSQL
เรียนรู้ว่าควรจัดเก็บข้อมูลที่ขูดมาในคลัง NoSQL แบบเอกสาร เช่น MongoDB เมื่อใดและอย่างไร เพื่อการจัดเก็บที่ยืดหยุ่นและมีโครงร่างไม่เคร่งครัด
การจัดเก็บข้อมูลในฐานข้อมูล NoSQL เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
When SQL Is Not Enough
Scraped data is often irregular: different pages yield different fields, nested structures, and evolving shapes. Rigid SQL schemas can be painful here.
NoSQL document databases store flexible JSON-like records, making them a natural fit for messy web data.
Documents and Collections
In a document store like MongoDB:
- A document is one JSON-like record.
- A collection groups related documents (like a table).
- Documents in one collection need not share the same fields.
{
"title": "Widget",
"price": 9.99,
"tags": ["tools", "sale"],
"vendor": { "name": "Acme", "rating": 4.5 }
}Connecting with PyMongo
The pymongo driver connects Python to MongoDB. Grab a database and a collection handle to start writing.
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017')
db = client['scraping']
products = db['products']Inserting Documents
Insert a single scraped record with insert_one or a batch with insert_many. MongoDB assigns an _id automatically.
record = {'title': 'Gadget', 'price': 14.5, 'tags': ['new']}
result = products.insert_one(record)
print(result.inserted_id)Avoiding Duplicates with Upsert
Re-running a scraper should not create duplicate rows. An upsert updates the matching document or inserts it if absent, keyed by a stable field like the product URL.
products.update_one(
{'url': record['url']},
{'$set': record},
upsert=True
)Unique Indexes
Enforce uniqueness at the database level with an index. This protects integrity even if your code has a bug.
products.create_index('url', unique=True)Querying Stored Data
Retrieve records with filter documents. Operators like $gt and $in express conditions.
cheap = products.find({'price': {'$lt': 10}})
for doc in cheap:
print(doc['title'], doc['price'])Storing Nested and Array Data
Unlike flat SQL columns, documents keep nested objects and arrays natively. This preserves the original structure of scraped pages without join tables.
review_doc = {
'product': 'Widget',
'reviews': [
{'user': 'a', 'stars': 5},
{'user': 'b', 'stars': 4}
]
}
db['catalog'].insert_one(review_doc)Bulk Writes for Speed
For large scrapes, batch operations dramatically reduce round trips. Collect writes and flush them together.
from pymongo import UpdateOne
ops = [UpdateOne({'url': r['url']}, {'$set': r}, upsert=True) for r in batch]
products.bulk_write(ops)SQL vs NoSQL for Scraping
Choose based on your data:
- NoSQL for variable, nested, fast-changing records.
- SQL when fields are stable and you need joins or strict constraints.
Many pipelines stage raw data in NoSQL, then transform into SQL for analysis.
Adding Timestamps and Metadata
Always stamp each scraped document with when it was captured and its source. This lets you track freshness, debug bad runs, and re-scrape stale records selectively.
from datetime import datetime
record['scraped_at'] = datetime.utcnow()
record['source'] = 'site.com'
products.insert_one(record)Quick Check
Test your understanding of NoSQL storage.
Recap
You learned to persist scraped data in NoSQL: documents and collections, connecting with PyMongo, upserts and unique indexes to prevent duplicates, querying, nested data, and bulk writes.
Document stores give scrapers flexible, scalable persistence.
เรียนรู้ Python ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การจัดเก็บข้อมูลในฐานข้อมูล NoSQL” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดเก็บข้อมูลในฐานข้อมูล NoSQL” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดเก็บข้อมูลในฐานข้อมูล NoSQL”
เรียนรู้ว่าควรจัดเก็บข้อมูลที่ขูดมาในคลัง NoSQL แบบเอกสาร เช่น MongoDB เมื่อใดและอย่างไร เพื่อการจัดเก็บที่ยืดหยุ่นและมีโครงร่างไม่เคร่งครัด คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดเก็บข้อมูลในฐานข้อมูล NoSQL” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดเก็บข้อมูลใน CSV/JSON
- การผสานรวมกับฐานข้อมูล (SQL)
- โซลูชันพื้นที่จัดเก็บบนคลาวด์
- การจัดเก็บข้อมูลในฐานข้อมูล NoSQL