[Solved] How can I debug "The Mongo server and the Meteor query disagree on how many documents match your query"?

Hi!

I’m getting the error:

The Mongo server and the Meteor query disagree on how many documents match your query. Cursor description:  CursorDescription {
collectionName: 'orders',
selector: { orderId: undefined },
options: { transform: [Function] } }

And I’ve looked all around the code to try to figure out when this is happening. I know which page that is triggering it.

How should I go about to narrowing down the problem?
Any suggestion would be helpful, because I’m stuck.

Is it when an collection gets publish, or is it when the client do a Orders.find(…)?

The problem is the undefined in your selector. Mongo and meteor treat these differently. The mongo driver removes the key, meteor tries to match literally undefined. It’s a horrible one to track down. I had it myself and was caused by updating to meteor 1.6

4 Likes

Thanks!

So how did you track it down?
Removing client lines or server lines of code?

Is it the client, that calls a Orders.find() with the bad parameter. So it’s in the client code I should go hunting?

Or is it in the server/publish code, do you think?

Meteor’s change log is a great source of information on understanding potential breaking changes before updating your project(s). The one @znewsham refers to is here.

It could be either - if you pass a selector directly through from client to server, you’ll end up with an empty selector - so it won’t be that. But if you pass through a parameter that you think is a string, and it ends up being undefined for some reason, that could cause it. Ultimately it will be somewhere on the server - but the argument could be coming from the client. Add to this, that it could in theory be a third party package making the subscription, and that the error thrown doesnt relate to the code that queries the DB - and this is a real nigthmare to find. I think I ended up patching Mongo.Collection.find and logging an error there if any of the arguments in the selector were undefined.

Thanks!
I do understand what causes it. But I don’t understand how to find it.

1 Like

When it happened to me, it was always in the client code. I would begin by logging whatever selector is being passed to Orders.find(…).

Edit: if that shows nothing abnormal, the next place is in your published cursor, which could have something like Meteor.publish('foo', () => Orders.find({ bar: undefined }));

1 Like

OK, now I have removed everything that has anything to do with orders on the client page, but it’s still there.

I need to restart the server and then call the page for the exception to get thrown.
Just a client refresh doesn’t provoke the error, so I guess it is something with publish.

Found it!

If this help other people:

I had a template that included another template that included a third.
The third template did a
Meter.publish('devices') on the client.
Which is not the collection that has the error (‘order’) above.

But on the server side in the publishing code
Meteor.publish('device')
There were a line of code with an Orders.find({orderId: device.orderId}) // device.orderId = undefined

I think I should have found it by a search for Orders.find(..) through the code but somehow I missed that code while searching.

So look for the code in publish on the server is my tip!

2 Likes

You were right with your second comment. Thanks!

I’m glad you managed to find it!