蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 db.collection.drop() 存在/缺失布尔返回值、同库集合隔离与不可逆清理验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-delete-collection.py
explore-delete-collection.py113 行
"""Offline MongoDB db.collection.drop() verifier (Ch15 delete-collection)."""
import json

VERSION = "CPython 3.12.14 offline MongoDB document/query verifier (mongod absent)"


def make_mydb():
    return {
        "mycol": {"docs": [{"_id": 1, "k": "a"}], "indexes": ["_id_"]},
        "mycol2": {"docs": [{"_id": 2, "k": "b"}, {"_id": 3, "k": "c"}], "indexes": ["_id_", "k_1"]},
        "demo": {"docs": [{"_id": 4, "k": "d"}], "indexes": ["_id_"]},
    }


def drop_collection(db: dict, name: str) -> bool:
    if name in db:
        del db[name]
        return True
    return False


def case_drop_existing_collection_returns_true():
    db = make_mydb()
    before = sorted(db.keys())
    dropped = drop_collection(db, "mycol2")
    after = sorted(db.keys())
    actual = {"before": before, "drop_return": dropped, "after": after}
    expected = {
        "before": ["demo", "mycol", "mycol2"],
        "drop_return": True,
        "after": ["demo", "mycol"],
    }
    return {
        "name": "drop_existing_collection_returns_true",
        "summary": "验证 db.mycol2.drop() 删除已存在集合返回 true 并从 show collections 移除。",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_drop_missing_collection_returns_false():
    db = make_mydb()
    res = drop_collection(db, "nonexistent_col")
    actual = {"drop_return": res, "collection_count": len(db)}
    expected = {"drop_return": False, "collection_count": 3}
    return {
        "name": "drop_missing_collection_returns_false",
        "summary": "验证对不存在的集合调用 drop() 返回 false 且不抛出异常。",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_drop_isolation_across_collections():
    db = make_mydb()
    drop_collection(db, "mycol2")
    actual = {
        "mycol_docs": db["mycol"]["docs"],
        "demo_docs": db["demo"]["docs"],
    }
    expected = {
        "mycol_docs": [{"_id": 1, "k": "a"}],
        "demo_docs": [{"_id": 4, "k": "d"}],
    }
    return {
        "name": "drop_isolation_across_collections",
        "summary": "验证删除 mycol2 不会波及同库中其他集合 mycol 与 demo 的数据。",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_irreversible_drop_and_recreate():
    db = make_mydb()
    drop_collection(db, "mycol2")
    after_drop_exists = "mycol2" in db
    # Recreate via fresh insert: old documents are permanently gone
    db["mycol2"] = {"docs": [{"_id": 100, "k": "fresh"}], "indexes": ["_id_"]}
    actual = {
        "existed_after_drop": after_drop_exists,
        "recreated_ids": [d["_id"] for d in db["mycol2"]["docs"]],
        "old_secondary_index_gone": "k_1" not in db["mycol2"]["indexes"],
    }
    expected = {
        "existed_after_drop": False,
        "recreated_ids": [100],
        "old_secondary_index_gone": True,
    }
    return {
        "name": "irreversible_drop_and_recreate",
        "summary": "验证 drop() 不可逆地清除旧文档与二级索引,后续重建集合从空状态开始。",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_drop_existing_collection_returns_true(),
        case_drop_missing_collection_returns_false(),
        case_drop_isolation_across_collections(),
        case_irreversible_drop_and_recreate(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/mongodb-delete-collection-lab/explore-delete-collection.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-delete-collection.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:MongoDB

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1741.zip

下载课程关联工程包:MongoDB

课程关联工程包(登记关联49章) · 含当前章节或实例 · mbskycom202612a1732.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习