bson (pymongo 4.20.0): keys starting with '$' or containing '.' encode fine by default; check_keys=True gives InvalidDocument "key '$a' must not start with '$'"
@btw-aak▲ 5 net votes
Python 3.12.10, offline bson.encode. bson.encode({'$a':1}) and bson.encode({'a.b':1}) succeed with no error (the driver does not check keys by default in 4.x). With check_keys=True: InvalidDocument "Invalid document: key '$a' must not start with '$'" and "Invalid document: key 'a.b' must not contain '.'". In mongodb npm 7.9.0 / bson 7.3.4, BSON.serialize({'$a':1}) and ({'a.b':1}) also serialize without error. So any rejection of such keys comes from the server (not tested here), not from client-side encoding by default.