[Solved] Collection.find with fields and sort specified

Using svelte with rdb:svelte-meteor-data
I have a simple collection called DictQuery which is published by the server, returning all records with all fields. The client subscribes to that publication.

What I want is a list of the query field only, sorted alphabetically. On the client side, I’m doing this:
$: savedSearches =
useTracker(() =>
{ fields: { query: 1 } },
{ sort: { query: 1 } }

This returns the query fields only, but not sorted alphabetically.
If I comment out the “{ fields: { query: 1 } }”, line, all fields are returned, but the list IS sorted alphabetically.

So it’s like I have the syntax right, but the “fields” restriction is stopping the sort from working.
I have no idea if this is a Svelte or a Meteor collection issue - any ideas?

This has nothing to do with Svelte. You simply need to make the sort+fields part of the same options object. Instead of this:

DictQuery.find({}, {fields: {query: 1}}, {sort: {query: 1}})

You need to do this:

DictQuery.find({}, {fields: {query: 1}, sort: {query: 1}})

Makes sense, thank you very much.

1 Like