[SERVER-15264] Dates shown in indexBounds are incorrect Created: 15/Sep/14  Updated: 02/Oct/14  Resolved: 01/Oct/14

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

Type: Bug Priority: Major - P3
Reporter: Riaz Ahmed Assignee: Ramon Fernandez Marina
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

> for(var a = 0 ;a < 100; a++){db.foo.insert({"a": new Date()})}
WriteResult({ "nInserted" : 1 })
> db.foo.ensureIndex({a:1})
> db.foo.find({a:{$gt: ISODate("2014-09-15T22:18:08.403Z")}}).explain()
{
        "cursor" : "BtreeCursor a_1",
        "isMultiKey" : false,
        "n" : 99,
        "nscannedObjects" : 99,
        "nscanned" : 99,
        "nscannedObjectsAllPlans" : 99,
        "nscannedAllPlans" : 99,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 1,
        "nChunkSkips" : 0,
        "millis" : 485,
        "indexBounds" : {
                "a" : [
                        [
                                ISODate("2014-09-15T22:18:08.403Z"),
                                ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")
                        ]
                ]
        },
        "server" : "testpc:27017",
        "filterSet" : false
}

Participants:

 Description   

MongoDB shell shows incorrect date when explain() is run with find() query on an indexed Date field.

1. Simply fill a collection with 100 documents containing a date field
2. Create an index on the date field.
3. Run a simple find query with explain() and see the indexBounds on the date field.

> for(var a = 0 ;a < 100; a++){db.foo.insert({"a": new Date()})}
WriteResult({ "nInserted" : 1 })
> db.foo.ensureIndex({a:1})
> db.foo.find({a:{$gt: ISODate("2014-09-15T22:18:08.403Z")}}).explain()
{
        "cursor" : "BtreeCursor a_1",
        "isMultiKey" : false,
        "n" : 99,
        "nscannedObjects" : 99,
        "nscanned" : 99,
        "nscannedObjectsAllPlans" : 99,
        "nscannedAllPlans" : 99,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 1,
        "nChunkSkips" : 0,
        "millis" : 485,
        "indexBounds" : {
                "a" : [
                        [
                                ISODate("2014-09-15T22:18:08.403Z"),
                                ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")
                        ]
                ]
        },
        "server" : "testpc:27017",
        "filterSet" : false
}



 Comments   
Comment by Ramon Fernandez Marina [ 02/Oct/14 ]

Thanks riyyaz, although credit goes to david.storch – he's the one that crafted the explanation I posted above.

Comment by Riaz Ahmed [ 02/Oct/14 ]

Thank you Ramon for your lucid explanation.

Best Regards,
Riaz Ahmed

Comment by Ramon Fernandez Marina [ 01/Oct/14 ]

riyyaz, this is the expected behavior. When you have a query {a: {$gt: b}} where the value of "b" is a date, this means that you should return all documents where "a" is a BSON element greater than "b", but also that "a" must be a date. These two conditions, converted into index bounds, give you the interval

(b, ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")]

The upper bound of the interval is simply the highest possible date value. So although it may look odd, as though a garbage date got displayed, this is in fact correct.

A similar argument applies to the $lt case:

db.foo.find({a:{$lt: ISODate("2014-09-15T22:18:08.403Z")}}).explain()
        ...
        "indexBounds" : {
                "a" : [
                        [
                                true,
                                ISODate("2014-09-15T22:18:08.403Z")
                        ]
                ]
        },
        ...

In this case, the left hand side of the interval is actually exclusive:

(true, b)

BSONType "true" is the type that compares just below BSONType date. The exclusive bound of "true" correctly leaves out all BSON elements that would compare less than the smallest date.

Please see comparison sort order of BSON types for more information.

Regards,
Ramón.

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