The Mongo server and the Meteor query disagree ($not $type)

I am hitting an interesting error (this is not due to a recent upgrade, it has been reproducible for a long time, I just now take the time to post about it):

W20210127-11:11:07.632(1)? (STDERR) The Mongo server and the Meteor query disagree on how many documents match your query. Cursor description:  CursorDescription {
W20210127-11:11:07.633(1)? (STDERR)   collectionName: 'X',
W20210127-11:11:07.634(1)? (STDERR)   selector: {
W20210127-11:11:07.634(1)? (STDERR)     isDone: true,
W20210127-11:11:07.634(1)? (STDERR)     '$or': [ [Object], [Object] ],
W20210127-11:11:07.634(1)? (STDERR)     owner: 'Y'
W20210127-11:11:07.634(1)? (STDERR)   },
W20210127-11:11:07.634(1)? (STDERR)   options: { transform: null }
W20210127-11:11:07.634(1)? (STDERR) }
I20210127-11:11:07.669(1)? Exception in defer callback: Error: The Mongo server and the Meteor query disagree on how many documents match your query. Maybe it is hitting a Mongo edge case? The query is: {"isDone":true,"$or":[{"price":{"$not":{"$type":1}}},{"price":{"$InfNaN":0}}],"owner":"Y"}
I20210127-11:11:07.669(1)?     at packages/mongo/oplog_observe_driver.js:884:15
I20210127-11:11:07.669(1)?     at Object.Meteor._noYieldsAllowed (packages/meteor.js:778:12)
I20210127-11:11:07.669(1)?     at OplogObserveDriver._publishNewResults (packages/mongo/oplog_observe_driver.js:851:12)
I20210127-11:11:07.669(1)?     at OplogObserveDriver._runQuery (packages/mongo/oplog_observe_driver.js:758:10)
I20210127-11:11:07.670(1)?     at OplogObserveDriver._runInitialQuery (packages/mongo/oplog_observe_driver.js:658:10)
I20210127-11:11:07.670(1)?     at packages/mongo/oplog_observe_driver.js:191:10
I20210127-11:11:07.670(1)?     at packages/mongo/oplog_observe_driver.js:15:9
I20210127-11:11:07.670(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1234:12)
I20210127-11:11:07.670(1)?     at packages/meteor.js:550:25
I20210127-11:11:07.670(1)?     at runWithEnvironment (packages/meteor.js:1286:24)

The query that causes this is the following

{
		isDone: true,
		$or: [{price: {$not: {$type: 1}}}, {price: Number.NaN}]
}

To circumvent the error, I defer the query to minimongo, where it works without problem.

Is this a know problem? Related to comparing to NaN maybe?

EDIT: @znewsham Woops it seems the cause of the error can be narrowed down to using price: {$not: {$type: 1}}.

I’ve seen this before when comparing to undefined. Meteor keeps the undefined but mongo removes it from the query so for {_id: undefined} meteor matches nothing and mongo matches everything! Really nasty if you have a variable query and a bug that sometimes causes undefined. I’ve not heard of it with NaN before. I wonder if it gets parsed out similar to undefined

1 Like