...
In the mongo shell (connecting to a 3.6 or a 4.0 mongod), update, delete and find queries don't seem to support the $code type. Insert operations however do allow $code, and all operations do support Code (instead of $code). Example: Insert works with Code and $code vladmac(mongod-3.6.4) test> db.foo.insert({"_id":Code("whatever")}) Inserted 1 record(s) in 50ms WriteResult({ "nInserted": 1 }) Delete, update and find work with Code but do not work with $code vladmac(mongod-3.6.4) test> db.foo.deleteOne({"_id":{"$code": "whatever"}}) 2018-08-29T15:40:53.500-0400 E QUERY [thread1] WriteError: unknown operator: $code : WriteError({ "index": 0, "code": 2, "errmsg": "unknown operator: $code", "op": { "q": { "_id": { "$code": "whatever" } }, "limit": 1 } }) WriteError@src/mongo/shell/bulk_api.js:466:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:846:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:910:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1154:21 DBCollection.prototype.deleteOne@src/mongo/shell/crud_api.js:363:17 @(shell):1:1
max.hirschhorn@10gen.com commented on Tue, 4 Sep 2018 21:53:25 +0000: Insert operations however do allow $code, and all operations do support Code (instead of $code). vladimir.isakov, the mongo shell doesn't meaningfully interpret extended JSON as part of its CRUD API. When you write {$code: "whatever"}, the BSON serialization is going to treat that as an Object (BSON type 0x03). The server doesn't prevent you from inserting a document with $-prefixed keys and the work to make it do so is tracked in SERVER-10987. > db.mycoll.insert({_id: "$code", value: {$code: "test"}}) WriteResult({ "nInserted" : 1 }) > db.mycoll.insert({_id: "Code", value: new Code("test")}) WriteResult({ "nInserted" : 1 }) > db.mycoll.insert({_id: "function", value: function() { }}) WriteResult({ "nInserted" : 1 }) > db.mycoll.find() { "_id" : "$code", "value" : { "$code" : "test" } } { "_id" : "Code", "value" : { "code" : "test" } } { "_id" : "function", "value" : { "code" : "function () { }" } } > db.mycoll.find({value: {$type: "javascript"}}) { "_id" : "Code", "value" : { "code" : "test" } } { "_id" : "function", "value" : { "code" : "function () { }" } } > db.mycoll.find({value: {$type: "object"}}) { "_id" : "$code", "value" : { "$code" : "test" } }