[SERVER-27630] illegal document insert sucess but find fucntion return error Created: 11/Jan/17  Updated: 11/Jan/17  Resolved: 11/Jan/17

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

Type: Bug Priority: Major - P3
Reporter: Malie Assignee: Mark Agarunov
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

var a = new Timestamp();
db.test.insert(a);
var b = new String("123");
db.test.insert(b);
res_malie:PRIMARY> db.test.find();

Participants:

 Description   

var a = new Timestamp();
db.test.insert(a);
mongo shell can run this insert command it doesn't need validate _id?
 
var b = new String("123");
db.test.insert(b)
this can also done
but if we run find() , it return error
res_malie:PRIMARY> db.test.find();
{ "_id" : ObjectId("57be5382bd9465a0ef04aaed"), "a" : 5, "b" : 5 }
{ "_id" : ObjectId("57be53b4bd9465a0ef04aaee"), "a" : 2 }
{ "_id" : ObjectId("57be5382bd9465a0ef04aaee") }
{ "_id" : ObjectId("5874e3dea1369cbc0d880fbf"), "timestamp" : Timestamp(1484055375, 0) }
{ "_id" : ObjectId("5874ead321ce10a26f6b7d9b"), "a" : "a" }
{ "_id" : ObjectId("5874ead621ce10a26f6b7d9c"), "a" : "A" }
Timestamp(0, 0)
Timestamp(0, 0)
Timestamp(0, 0)
Timestamp(0, 0)
2017-01-11T14:20:31.084+0800 E QUERY    [thread1] SyntaxError: missing ] after element list :
tojsonObject@src/mongo/shell/types.js:708:13
tojson@src/mongo/shell/types.js:653:21
DBQuery.prototype.shellPrint@src/mongo/shell/query.js:533:63
shellPrintHelper@src/mongo/shell/utils.js:490:1
@(shell2):1:1
 
Error: SyntaxError: missing ] after element list :
tojsonObject@src/mongo/shell/types.js:708:13
tojson@src/mongo/shell/types.js:653:21
DBQuery.prototype.shellPrint@src/mongo/shell/query.js:533:63
shellPrintHelper@src/mongo/shell/utils.js:490:1
@(shell2):1:1
 
 
 
I found in mongorestore and other tools have a --objcheck default option
 
but mongod I find nothing
so
it's a mongo shell bug ?



 Comments   
Comment by Mark Agarunov [ 11/Jan/17 ]

Hello 402309718@qq.com,

Thanks for reporting this issue. The behavior you are seeing is due to a known issue with the mongo shell. To mitigate this issue, Javascript protection was introduced, and is enabled by default starting with MongoDB 3.4. To enable this with an earlier version of the mongo shell, you may pass the `--enableJavascriptProtection` flag to the mongo shell. With this flag set, the documents are returned as expected:

[~] mongo --enableJavaScriptProtection                                                                                                                                                           14:30:57
MongoDB shell version: 3.2.11
connecting to: test
> db.test.drop()
true
> var a = new Timestamp();
> db.test.insert(a);
WriteResult({ "nInserted" : 1 })
> var b = new String("123");
> db.test.insert(b);
WriteResult({ "nInserted" : 1 })
> db.test.find();
[object]
{ "_id" : ObjectId("58768803a87fe4645a4a6a20"), "0" : "1", "1" : "2", "2" : "3", "ltrim" : { "code" : "function trimLeft() {\n    [native code]\n}" }, "rtrim" : { "code" : "function trimRight() {\n    [native code]\n}" }, "includes" : { "code" : "function () {\n        'use strict';\n        return String.prototype.indexOf.apply(this, arguments) !== -1;\n    }" }, "pad" : { "code" : "function (length, right, chr) {\n    if (typeof chr == 'undefined')\n        chr = ' ';\n    var str = this;\n    for (var i = length - str.length; i > 0; i--) {\n        if (right) {\n            str = str + chr;\n        } else {\n            str = chr + str;\n        }\n    }\n    return str;\n}" } }
>

Regarding your concern with the ability to insert these javascript objects, this is an expected behavior of MongoDB, though it may look unusual due to the formatting of the output on the shell.

Thanks,
Mark

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