"""Offline MongoDB index limits, query restrictions & 1024-byte key boundary verifier."""
import json
VERSION = "CPython 3.12.14 offline MongoDB document/query verifier (mongod absent)"
MAX_INDEXES_PER_COLLECTION = 64
MAX_COMPOUND_FIELDS_LEGACY = 31
MAX_COMPOUND_FIELDS_MODERN = 32
LEGACY_MAX_INDEX_KEY_BYTES = 1024
LEGACY_MAX_INDEX_NAME_BYTES = 128
def check_index_limits(existing_count, field_names, index_name):
errors = []
if existing_count >= MAX_INDEXES_PER_COLLECTION:
errors.append("max_64_indexes_exceeded")
if len(field_names) > MAX_COMPOUND_FIELDS_MODERN:
errors.append("max_compound_fields_exceeded")
if len(index_name.encode("utf-8")) > LEGACY_MAX_INDEX_NAME_BYTES:
errors.append("legacy_128_byte_name_exceeded")
return {"allowed": len(errors) == 0, "errors": errors}
def check_legacy_key_size(value_str):
# BSON index key payload check for MongoDB 2.6-4.0 (removed in 4.2+ WiredTiger)
size_bytes = len(value_str.encode("utf-8"))
return {"bytes": size_bytes, "within_legacy_1024_limit": size_bytes < LEGACY_MAX_INDEX_KEY_BYTES}
def query_uses_standard_btree_index(op_name, pattern=None):
if op_name in {"$nin", "$not", "$mod", "$where"}:
return False
if op_name == "$regex":
return bool(pattern and pattern.startswith("^"))
return op_name in {"$eq", "$gt", "$gte", "$lt", "$lte", "$in"}
def main():
# Case 1: Hard structural limits (64 indexes, 31/32 compound fields, 128-byte historical name limit)
ok_res = check_index_limits(10, ["a", "b"], "a_1_b_1")
over_64 = check_index_limits(64, ["a"], "a_1")
over_fields = check_index_limits(5, [f"f{i}" for i in range(33)], "compound_33")
over_name = check_index_limits(5, ["x"], "x" * 130)
c1_actual = {
"valid_index_allowed": ok_res["allowed"],
"index_65_errors": over_64["errors"],
"compound_33_errors": over_fields["errors"],
"name_130_errors": over_name["errors"],
"legacy_vs_modern_compound_cap": [MAX_COMPOUND_FIELDS_LEGACY, MAX_COMPOUND_FIELDS_MODERN],
}
c1_expected = {
"valid_index_allowed": True,
"index_65_errors": ["max_64_indexes_exceeded"],
"compound_33_errors": ["max_compound_fields_exceeded"],
"name_130_errors": ["legacy_128_byte_name_exceeded"],
"legacy_vs_modern_compound_cap": [31, 32],
}
# Case 2: Legacy 1024-byte key size boundary (MongoDB 2.6-4.0 vs 4.2+)
small_key = check_legacy_key_size("a" * 512)
oversized_key = check_legacy_key_size("a" * 1200)
c2_actual = {
"small_key_ok": small_key["within_legacy_1024_limit"],
"oversized_key_ok": oversized_key["within_legacy_1024_limit"],
"oversized_bytes": oversized_key["bytes"],
}
c2_expected = {
"small_key_ok": True,
"oversized_key_ok": False,
"oversized_bytes": 1200,
}
# Case 3: Query operators that cannot use standard B-tree index efficiently
ops = {
"$eq": query_uses_standard_btree_index("$eq"),
"$nin": query_uses_standard_btree_index("$nin"),
"$not": query_uses_standard_btree_index("$not"),
"$mod": query_uses_standard_btree_index("$mod"),
"$where": query_uses_standard_btree_index("$where"),
"regex_unanchored": query_uses_standard_btree_index("$regex", "mongo"),
"regex_prefix": query_uses_standard_btree_index("$regex", "^mongo"),
}
c3_actual = ops
c3_expected = {
"$eq": True,
"$nin": False,
"$not": False,
"$mod": False,
"$where": False,
"regex_unanchored": False,
"regex_prefix": True,
}
cases = [
{
"name": "collection_index_and_compound_limits",
"summary": "Verify 64-index cap, 31/32 compound field limit, and 128-byte historical index name limit.",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "legacy_1024_byte_key_limit_boundary",
"summary": "Verify 1024-byte legacy index key limit rejects 1200-byte keys while accepting 512-byte keys.",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "non_indexable_query_operators",
"summary": "Verify $nin, $not, $mod, $where, and unanchored regex bypass standard index scans unlike $eq and ^prefix regex.",
"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-indexing-limitations-lab/explore-indexing-limitations.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-indexing-limitations.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线文档/管道/索引/GridFS/ObjectId 验证环境(本地未安装 mongod/mongosh 服务端)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1745.zip
课程关联工程包(登记关联49章) · 含当前章节或实例 · mbskycom202612a1732.zip