[SERVER-36984] Find, update and delete operations in the shell don't support $code Created: 04/Sep/18  Updated: 27/Oct/23  Resolved: 05/Sep/18

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Vladimir Isakov (Inactive) Assignee: Max Hirschhorn
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

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



 Comments   
Comment by Max Hirschhorn [ 04/Sep/18 ]

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" } }

Generated at Thu Feb 08 04:44:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.