MongoDB 3.2 – Strict Mode Not Working

mongodbmongodb-3.2

I was test in process of testing the strict mode of mongo shell queries. This the link i am referring.
Sample document looks like –

   {  "_id" : ObjectId("19876"),
    "ip" : "127.0.0.1",
    "pgDownloadTime" : NumberLong(25000),
    "domain" : "test.domain.com",
    "pgAccessTime" : NumberLong("1469422166381"),
    "pgAccessMin" : NumberLong(1469422140),
    "pgAccessHour" : NumberLong(1469421000),
    "contentType" : 1,
    "tags" : [
            "testTag1",
            "testTag2"
    ],
    "catIds" : [
            "cat1",
            "cat2"
    ]

Query using strict mode is –

db.testData.find({ "pgAccessMin" : { "$gte" : { "$numberLong" : "1469406600" }, "$lt" : { "$numberLong" : "1469413800" } }, "domain" : "test.domain.com" })

And Query without using strict mode is –

db.testData.find({ "pgAccessMin" : { "$gte" : 1469406600, "$lt" :1469413800 }, "domain" : "test.domain.com" })

Issue is that Strict mode query is not providing results but query without strict mode provides the data.
I am using mongo version – 3.2.7

Best Answer

The MongoDB Extended JSON format allows representation of additional BSON data types that are not part of standard JSON.

There is a subtle distinction between:

  • being able to parse the extended JSON into a JavaScript representation
  • the parser then taking the additional step of interpreting the extended key/value pairs into an object with associated JavaScript wrappers such as NumberLong() or Date()

As per the MongoDB Extended JSON page you linked to, the mongo shell can parse the strict mode Extended JSON (which conforms to the JSON standard) but without recognition of type information:

Other JSON parsers, including mongo shell and db.eval(), can parse strict mode representations as key/value pairs, but without recognition of the type information.

The strict mode is only only intended as a data interchange format (for example, transferring a collection via mongoexport and mongoimport).

For queries in the mongo shell you should use the mongo Shell Mode representations as noted in the MongoDB Extended JSON documentation.