"""Offline legacy PHP 5 `mongo` driver CRUD contract & PHP 7/8 migration verifier."""
import json
VERSION = "CPython 3.12.14 offline MongoDB document/query verifier (mongod absent)"
class LegacyMongoCollection:
def __init__(self, name):
self.name = name
self.docs = []
def insert(self, doc):
self.docs.append(dict(doc))
return True
def find(self):
return [dict(d) for d in self.docs]
def update(self, criteria, modifier):
updated = 0
for d in self.docs:
if all(d.get(k) == v for k, v in criteria.items()):
if "$set" in modifier:
d.update(modifier["$set"])
updated += 1
break # legacy default updates first matching document unless multiple=true
return updated
def remove(self, criteria, just_one=False):
removed = 0
kept = []
for d in self.docs:
matched = all(d.get(k) == v for k, v in criteria.items())
if matched and (not just_one or removed == 0):
removed += 1
else:
kept.append(d)
self.docs = kept
return removed
def main():
# Case 1: Connection defaults & collection creation
default_uri = "mongodb://localhost:27017"
col = LegacyMongoCollection("demo_articles")
c1_actual = {"default_uri": default_uri, "db": "test", "collection": col.name}
c1_expected = {"default_uri": "mongodb://localhost:27017", "db": "test", "collection": "demo_articles"}
# Case 2: insert() + find() iteration (fixing associative key typo "by" => "DevDocs")
col.insert({
"title": "MongoDB",
"description": "database",
"likes": 100,
"url": "https://example.org/mongodb/",
"by": "DevDocs",
})
col.insert({
"title": "MongoDB 教程",
"description": "second copy",
"likes": 50,
"url": "https://example.org/mongodb-2/",
"by": "DevDocs",
})
titles_before = [d["title"] for d in col.find()]
c2_actual = {"count": len(titles_before), "titles": titles_before}
c2_expected = {"count": 2, "titles": ["MongoDB", "MongoDB 教程"]}
# Case 3: update($set) and remove(justOne=True) behavior + modern migration mapping
mod_count = col.update({"title": "MongoDB"}, {"$set": {"title": "MongoDB 教程"}})
titles_after_update = [d["title"] for d in col.find()]
removed_count = col.remove({"title": "MongoDB 教程"}, just_one=True)
remaining_count = len(col.find())
migration_map = {
"MongoClient": "MongoDB\\Client (mongodb/mongodb) or MongoDB\\Driver\\Manager",
"insert": "insertOne / insertMany / BulkWrite::insert",
"update": "updateOne / updateMany / BulkWrite::update",
"remove": "deleteOne / deleteMany / BulkWrite::delete",
}
c3_actual = {
"modified": mod_count,
"titles_after_update": titles_after_update,
"removed_with_just_one": removed_count,
"remaining_after_just_one": remaining_count,
"migration_targets": sorted(migration_map.keys()),
}
c3_expected = {
"modified": 1,
"titles_after_update": ["MongoDB 教程", "MongoDB 教程"],
"removed_with_just_one": 1,
"remaining_after_just_one": 1,
"migration_targets": ["MongoClient", "insert", "remove", "update"],
}
cases = [
{
"name": "legacy_php_connect_and_create_collection",
"summary": "Verify legacy MongoClient default localhost:27017 connection and collection handle selection.",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "legacy_php_insert_and_find",
"summary": "Verify associative document insert() and cursor title iteration.",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "legacy_php_update_remove_and_migration",
"summary": "Verify $set update, remove(justOne=true) deleting only one match, and PHP 7/8 API migration mapping.",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线文档/管道/索引/GridFS/ObjectId 验证环境(本地未安装 mongod/mongosh 服务端)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线文档/管道/索引/GridFS/ObjectId 验证环境(本地未安装 mongod/mongosh 服务端)。
入口文件:projects/mongodb-php-lab/explore-php.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-php.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线文档/管道/索引/GridFS/ObjectId 验证环境(本地未安装 mongod/mongosh 服务端)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联49章) · 含当前章节或实例 · mbskycom202612a1732.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1761.zip