modeller
modeller

Reputation: 3850

What is the difference between find({}, {sort: ...}) and find().sort(...)?

In MongoDB documentation, when I search for sort, it direct me to the cursor.sort() page. (btw the documentation doesn't specify what is returned out of this method.). So I used it in my meteor script Collection.find().sort('date':1), but got complained that find().sort is not a function. (I thought find() does returns a cursor, isn't it?)

So I did some further search, and found some tutorials tell me to use find({}, {sort: ...}).

So what is the difference between these two methods?

Upvotes: 0

Views: 572

Answers (2)

JeremyK
JeremyK

Reputation: 3240

In the Meteor framework, some things you need to do the Meteor way!
Just use Collection.find as specified in the Meteor Docs, and pass a Sort Specifier.

What is the difference between the two?
One has been wrapped by Meteor, and works inside the framework, the other one doesn't!

I don't believe you will see any performance difference between 'the Meteor api' from in the framework, or 'the standard MongoDB api' from (non meteor) nodejs.

Upvotes: 1

Mikkel
Mikkel

Reputation: 7777

Using find({}, sort... Asks Mongo to do the sorting, and this is the most efficient way because the database server can optimise a sort if a field is indexed.

Meteor doesn't provide the full Mongo api, because mini Mongo in the browser does have all the features and they want to provide a consistent api in both client and server.

I haven't checked it but I think if you add a fetch () in between the find and the sort it will work because fetch will return an array which is sortable

Upvotes: 2

Related Questions