[SERVER-11530] timestamp vs. date errors Created: 01/Nov/13  Updated: 10/Dec/14  Resolved: 08/Nov/13

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.4.8, 2.5.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: David Storch Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: 26qa
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-3304 Change comparison order so all Dates ... Closed
Operating System: ALL
Participants:

 Description   

1) Repro with a find()

> db.x.drop();
true
> db.x.insert({a: new Date(1290283)})
> db.x.find({a: Timestamp(12383948,0)})
error: { "$err" : "wrong type for field (a) 17 != 9", "code" : 13111 }

2) Repro with an update()

> db.x.drop()
true
> db.x.insert({a: new Date(), n: 0})
> db.x.update({n:0}, {$set: {a: Timestamp(1,0)}})
wrong type for field (a) 17 != 9



 Comments   
Comment by Daniel Pasette (Inactive) [ 08/Nov/13 ]

SERVER-3304

Comment by Andrew Morrow (Inactive) [ 01/Nov/13 ]

The problem is not specific to update, it is a more general issue with BSONElement::woCompare's handling of Date and Timestamp.

The BSON 'Date' and 'Timestamp' types have the same 'canonical type', 45: https://github.com/mongodb/mongo/blob/471bc7a3de80fae0551f164a4a0d51e2325cae30/src/mongo/bson/bsontypes.h#L143-L145

This means that if you pass a BSONElement containing a 'Date' and a BSONElement containing a 'Timestamp' to BSONElement::woCompare it will fail, since in BSONElement::woCompare the elements are forwarded to 'compareElementValues' if they have the same canonicalized type, which 'Date' and 'Timestamp' do:

https://github.com/mongodb/mongo/blob/471bc7a3de80fae0551f164a4a0d51e2325cae30/src/mongo/bson/bson-inl.h#L155-L165

However, compareElementValues requires that the elements have the same type, unless they are numeric (which Date and Timestamp are not):

https://github.com/mongodb/mongo/blob/471bc7a3de80fae0551f164a4a0d51e2325cae30/src/mongo/bson/bson-inl.h#L34-L38

As a result of breaking this invariant, when invoking BSONElement::woCompare between a Date and Timestamp element, the timestamp element will be incorrectly accessed as a date:

https://github.com/mongodb/mongo/blob/471bc7a3de80fae0551f164a4a0d51e2325cae30/src/mongo/bson/bson-inl.h#L56-L60

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