Adding fields to client side docs only. Custom sort function

83 views
Skip to first unread message

Eliezer Steinbock

unread,
Oct 27, 2014, 12:19:02 PM10/27/14
to meteo...@googlegroups.com
I'd like to able to sort and filter docs by custom functions that could depend on other fields in the docs or even other collections.

For example, sort the docs in a collection using the function a - b where a and b are fields belonging to the docs in the collection.

I'm thinking it would be a good idea to add a new field to document on the client (equal to a - b in the above example), and then sorting according to that field.

The two questions I have are:
1 - is this a good solution? Is there a better a solution?
2 - how do I only add fields to client side documents? If I do an update on a client side document, it might work for a second, but the server will then reject the update (and I want the server to reject it).

One last point, denormalizing the data won't help me here. The custom sorting function will be different for every user.

tom1v...@gmail.com

unread,
Oct 27, 2014, 12:53:15 PM10/27/14
to meteo...@googlegroups.com
How many documents are you providing this feature for?

Eliezer Steinbock

unread,
Oct 27, 2014, 12:54:51 PM10/27/14
to meteo...@googlegroups.com

700

On 27 Oct 2014 18:53, <tom1v...@gmail.com> wrote:
How many documents are you providing this feature for?

--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/5CLEvNsEVes/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tom1v...@gmail.com

unread,
Oct 27, 2014, 1:02:47 PM10/27/14
to meteo...@googlegroups.com
Not knowing more about your application, here's my suggestion:

Send the function to the server using a Meteor Method. Then on the server side create a new collection for the user (or document subset labelled for that user), with documents: {originalDocumentId, functionValue}. Then publish those documents to the client.

The reason is that you don't want to have to send the 700 documents to the client.

Eliezer Steinbock

unread,
Oct 27, 2014, 1:11:42 PM10/27/14
to meteo...@googlegroups.com

The 700 documents are already on the client and I'm happy with this solution. I don't want users waiting between every time they want to see a few more documents.

To make it more clear what I'm talking about. I have a fantasy football website and one of the pages has a table of players. Displayed 10 at a time by default. Users can sort players in all sorts of different ways, but I'd like to be able to add the ability to sort in more complex ways in the future.

--

tom1v...@gmail.com

unread,
Oct 27, 2014, 1:21:48 PM10/27/14
to meteo...@googlegroups.com
OK. Then you can create a mapping like I said but on the client side without creating a collection, just an array that you sort.

You won't be able to add and sort a key+value using MiniMongo only on the client side.

Eliezer Steinbock

unread,
Oct 27, 2014, 1:33:06 PM10/27/14
to meteo...@googlegroups.com

Hmmm... Seems slightly annoying that that isn't possible. Doesn't seem to complicated.

All the other sorts I do, do use mini mongo, so I wonder how much doing a custom sort with a new array will mess with the cleanness of the code. I'll have to play around with it.

On second thoughts, it might not actually be such a problem.

On 27 Oct 2014 19:21, <tom1v...@gmail.com> wrote:
OK. Then you can create a mapping like I said but on the client side without creating a collection, just an array that you sort.

You won't be able to add and sort a key+value using MiniMongo only on the client side.

--

Kelly Copley

unread,
Oct 27, 2014, 2:09:35 PM10/27/14
to meteo...@googlegroups.com
I think this could be accomplished with the low level publication API


aCollection = new Meteor.Collection("aCollection");


/server
Meteor.publish('namedPublish', function () {
aCollection.find().observe({
added: function (document) {
//modify the document
document.c = document.a + document.b;
//publish to client
self.added('clientCollection', document._id, document);
},
changed: function (newDocument, oldDocument) {
//modify the document
document.c = document.a + document.b;
//publish changes to the client
self.changed('clientCollection', oldDocument._id, newDocument);
},
removed: function (oldDocument) {
//remove the document
self.removed('clientCollection', oldDocument._id);
}
});
});

//client
Meteor.subscribe('namedPublish');

Eliezer Steinbock

unread,
Oct 27, 2014, 4:24:13 PM10/27/14
to meteo...@googlegroups.com

Thanks. Is there a way of doing it client side only? I'd rather use the client's computing power rather than the server's if I could.

Bob Monsour

unread,
Oct 28, 2014, 10:16:57 AM10/28/14
to meteo...@googlegroups.com
You can create a client-side only collection by specifying the name as null.

```
new Mongo.Collection(null);
```
You'd then have to copy your subscribed collection into this new one, but then you'd have all the minimongo functionality available to it.

Not sure if that's a fit with what you're trying to do.
Reply all
Reply to author
Forward
0 new messages