MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _dict_to_bson

Function _dict_to_bson

bson/__init__.py:997–1017  ·  view source on GitHub ↗

Encode a document to BSON.

(
    doc: Any, check_keys: bool, opts: CodecOptions[Any], top_level: bool = True
)

Source from the content-addressed store, hash-verified

995
996
997def _dict_to_bson(
998 doc: Any, check_keys: bool, opts: CodecOptions[Any], top_level: bool = True
999) -> bytes:
1000 """Encode a document to BSON."""
1001 if _raw_document_class(doc):
1002 return cast(bytes, doc.raw)
1003 try:
1004 elements = []
1005 if top_level and "_id" in doc:
1006 elements.append(_name_value_to_bson(b"_id\x00", doc["_id"], check_keys, opts))
1007 for key, value in doc.items():
1008 if not top_level or key != "_id":
1009 try:
1010 elements.append(_element_to_bson(key, value, check_keys, opts))
1011 except InvalidDocument as err:
1012 raise InvalidDocument(f"Invalid document: {err}", doc) from err
1013 except AttributeError:
1014 raise TypeError(f"encoder expected a mapping type but got: {doc!r}") from None
1015
1016 encoded = b"".join(elements)
1017 return _PACK_INT(len(encoded) + 5) + encoded + b"\x00"
1018
1019
1020if _USE_C:

Callers 15

test__dict_to_bsonMethod · 0.90
test__dict_to_bsonMethod · 0.90
_op_msg_no_headerFunction · 0.90
_query_implFunction · 0.90
_batched_op_msg_implFunction · 0.90
collection_infoMethod · 0.90
__init__Method · 0.90
encryptMethod · 0.90
collection_infoMethod · 0.90
__init__Method · 0.90

Calls 6

_raw_document_classFunction · 0.90
InvalidDocumentClass · 0.90
_name_value_to_bsonFunction · 0.85
_element_to_bsonFunction · 0.85
itemsMethod · 0.80
joinMethod · 0.45

Tested by 2

test__dict_to_bsonMethod · 0.72
test__dict_to_bsonMethod · 0.72