libbson's bson-to-json converts the BSON Javascript type to just:
{ "foo" : "function foo(bar) { return bar; }" }
(Document with single ```foo``` key).
On the way from json-to-bson, there is no way to distinguish this from a (key => string) pair, and hence, libbson does not create a BSON Javascript type.
I would suggest to use the "extended JSON"'s serialisation way:
"CodeWithScope": { "$code": "function(){}", "$scope": {} },
- is related to
-
CDRIVER-1335 libbson does not properly serialize the Code type to JSON
- Closed
- related to
-
CDRIVER-1364 BSON type "JavaScript code w/ scope" cannot be reliably roundtripped
- Closed