--- id: python-mongodb-create-db title: "Python MongoDB Create DB" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["pymongo", "MongoClient", "파이썬 MongoDB 데이터베이스 생성"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["python", "mongodb", "w3schools", "database", "pymongo"] raw_sources: ["https://www.w3schools.com/python/python_mongodb_create_db.asp"] applied_in: [] github_commit: "" --- # [[Python MongoDB Create DB]] ## 🎯 한 줄 통찰 (One-line insight) Unlike MySQL's explicit `CREATE DATABASE`, MongoDB is lazy — `myclient["mydatabase"]` doesn't actually create anything on the server until a collection with at least one document is inserted into it. [S1] ## 🧠 핵심 개념 (Core concepts) - **`pymongo.MongoClient(url)`** — connects to a MongoDB server. [S1] - **`myclient["dbname"]`** — references (but does not yet create) a database. [S1] - **Lazy creation** — MongoDB doesn't create the database until a collection with content exists. [S1] - **`myclient.list_database_names()`** — lists all databases, used to check existence. [S1] ## 🧩 추출된 패턴 (Extracted patterns) - **Content-triggered creation is the opposite of SQL** — where `CREATE DATABASE` is an explicit imperative command in SQL, MongoDB treats database existence as a side effect of inserting data, not a standalone action. [S1] ## 📖 세부 내용 (Details) - Connect and reference a database: `myclient = pymongo.MongoClient("mongodb://localhost:27017/"); mydb = myclient["mydatabase"]`. [S1] - List all databases: `print(myclient.list_database_names())`. [S1] - Check specific database: `dblist = myclient.list_database_names(); if "mydatabase" in dblist: print("The database exists.")`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **SQL과의 근본적 차이**: SQL은 `CREATE DATABASE`로 즉시 생성되지만, MongoDB는 컬렉션+문서가 실제로 삽입되기 전까지 데이터베이스가 생성되지 않는다. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — 다음 챕터(Create Collection, Insert)를 완료해야 실제로 데이터베이스가 존재하게 된다. [S1] ## 💻 코드 패턴 (Code patterns) Connect to MongoDB and reference a database (Python): ```python import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.90 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[Python Tutorial]] - **관련 개념:** [[Python MongoDB Create Collection]], [[Python MySQL Create Database]] - **참조 맥락:** MongoDB의 지연 생성(lazy creation) 특성 — MySQL의 즉시 생성 방식과 대비. ## 📚 출처 (Sources) - [S1] W3Schools — Python MongoDB Create Database — https://www.w3schools.com/python/python_mongodb_create_db.asp ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "Python MongoDB Create Database" page (Astra wiki-curation, P-Reinforce v3.1 format).