MongoDB slow FindOne vs Find

I’m trying to follow this blog post on how to improve access times by the factor 2 when using findOne vs find:

However when I apply it on my back-end (server folder) I get the following error message:

TypeError: Object [object Object] has no method 'limit'

Strangely, the same command works directly in the command line without problems. This is the line of code:

 var resultFind = Scrape.find({userId: scrapeUser, screen: "selectScreen"}, {_id:1}).limit(1);

Using the same line of code except the .limit(1) returns exactly as wanted. What am I missing here? BTW, it didn’t work for another collection either, tried it on Meteor.users (again, works in command line without problems)

meteorized-mongo syntax != mongo syntax. Meteor has morphed it a bit when integrating.

You should include the limit and fields in your options {fields: {_id: 1}, …}, as in the docs link.

1 Like

I just found that out as well, however when I change it to:

var result = Meteor.users.find({ email: gmEmail }, { _id: 1 , limit: 1});

Then I get a new error message:

TypeError: Converting circular structure to JSON

you should really use {fields: {_id: 1} … } instead of just {_id: 1}

not sure about that error btw

I followed your advice and changed it to:

var result = Meteor.users.find({ email: gmEmail }, { fields: {_id: 1 , limit: 1}});

However I get the same error message as before (btw, the doc is useless as it doesn’t give any example for the correct syntax).

I also tried:

var result = Meteor.users.find({ email: gmEmail }, { fields: {_id: 1 }, limit: 1});

With the same error message. It’s not addressing the error message. Any other idea?

OMG, call me stupid. The error was actually in the next line, overlooked it. It was caused by the JSON.stringify I had there

right because fudging the [options] to the query in that way doesnt produce an error, still though, you’ll need to use {fields: …} syntax

The doc does have several examples btw, just look up "fields: " or "limit: " also, in the link I sent you, they explicly say the fields on the [options] object :smile:

personally i’ve found meteor docs to be of high quality xD


your second line of code is correct. the first is incorrect, because it will interpret “limit” to be a field on your documents. sorry lol, i abuse editing …

As a side note, when you use the limit option in Meteor-Mongo it’s a good practice to use the sort option too:

Sorting in a publish function isn’t usually necessary unless the result of the sort changes which documents are sent (e.g. you are using a limit).

Thanks for the informative link, @massimosgrelli

Please note that it’s not important for me as these collections aren’t published to the client. Its all for back-end purposes only

1 Like