I have documents that look like this:
{
"_id" : "cgR25FAxb3tbYShjN",
"owner" : "6mQKnNnwkQYSaaAMr",
"username" : "admin",
"filename" : "5sQqwbDFBcZZ.png",
"title" : "Title",
"points" : 2,
"loc" : {
"type" : "Point",
"coordinates" : [
-119.981134343,
20.635934343
]
},
"createdAt" : ISODate("2016-09-15T10:27:20.956Z")
}
I want to search for the closest 100 documents, then sort by either points
or createdAt
. I want these documents to be returned to the client in an ‘infinite scroll’ style. I can get those closest 100 documents by this query:
Photos.find({loc: {$near: { $geometry: {type: "Point", coordinates: [lng, lat]}}}}, {limit: 100});
The Problem:
If I do the standard ‘infinite scroll’ that Meteor suggests, it’s going to be troublesome to sort on the client. It will sort the first 20 (or however many you request at a time) documents by points, but then the second 20 will be either sorted in its own order, or documents will pop in at places the user doesn’t expect.
I don’t think that passing all 100 documents to the client at once is a good idea either.
Is it possible to make a ‘custom’ publication? For example, create an array of 100 documents from the query above, then return 20 items of the array at a time. If I did it this way does that mean I would be storing 100 documents in memory for each user? That doesn’t seem optimal either.
Thoughts? Thanks!!