I came across a problem in the node.js driver and I was told that this is a server question (https://github.com/mongodb/node-mongodb-native/issues/811).
I tried to recreate the problem in the mongo shell, but was not able to, so here what I did in the node.js environment and what the problem is. To recreate:
1. Insert Document:
{"x": new Code("function () {}", {a: 55}), "comment": "Code w/scope a:55"}
2. Run query:
{"x": new Code("function () {}", {b: 77})}
3. Observe that the document from 1. is returned even though the scope does not match.
Problem:
It appears that the query for code only matches the function part, but not the scope part. Is this a bug? If not, how to I rewrite the query so that I get back only matching scopes? Or is this a problem to be solved in the client, meaning, I have to do the selection for the scope myself?