[SERVER-14252] Able to update documents to have $ref $id and $db fields on top level Created: 13/Jun/14  Updated: 06/Dec/22  Resolved: 20/Jul/22

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.6.4, 2.7.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andrew Emil (Inactive) Assignee: Backlog - Query Optimization
Resolution: Done Votes: 0
Labels: 26qa, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-14244 Additional tests for setting fields w... Closed
Assigned Teams:
Query Optimization
Operating System: ALL
Participants:

 Description   

It seems that it is possible to update illegal top level fields ($ref, $id, $db). Note that this fails if you use insert, instead of update.

> db.test.drop()
true
> db.test.update({_id:0}, {_id:0, $ref: "1", $id: 1, $db:"1"}, true)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 0 })
> db.test.findOne()
{ "_id" : 0, "$ref" : "1", "$id" : 1, "$db" : "1" }
> db.test.insert({_id: 1, $ref: "1", $id: 1, $db: "1"})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 2,
                "errmsg" : "Document can't have $ prefixed field names: $ref"
        }
})
> db.test.find()
{ "_id" : 0, "$ref" : "1", "$id" : 1, "$db" : "1" }
> db.runCommand({buildinfo:1})
{
        "version" : "2.7.2-pre-",
        "gitVersion" : "87d09bc9cfe898f093c4d76ca5aad807c2e817e7",
        "OpenSSLVersion" : "",
        "sysInfo" : "Linux localhost.localdomain 3.13.10-200.fc20.x86_64 #1 SMP Mon Apr 14 20:34:16 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49",
        "loaderFlags" : "-fPIC -pthread -Wl,-z,now -rdynamic",
        "compilerFlags" : "-Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -fno-builtin-memcmp -std=c99",
        "allocator" : "tcmalloc",
        "versionArray" : [
                2,
                7,
                2,
                -100
        ],
        "javascriptEngine" : "V8",
        "bits" : 64,
        "debug" : false,
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}
> 



 Comments   
Comment by James Wahlin [ 20/Jul/22 ]

Closing as it is now legal to both insert and update fields with leading "$" character.

Generated at Thu Feb 08 03:34:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.