Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor, minimongo not sorting on date properly, even with ISO8601 data format

Problem

I have this page on my meteor site: twbrewing.com/blog and on it I want to sort the blog posts by date.

If I do the following in the console:

BlogPosts.find({}, {sort: {date: -1, time: -1}})

It returns the posts in the wrong order. Which is also confirmed by the incorrect display order of the posts.

The date of the first doc returned in the collection is:

date: "2013-12-16"

while the date of the second post is:

date: "2014-01-02"

I believe this is proper ISO 8601 format so I am not really sure why its failing.

Additional code

I do publish from the server:

Meteor.publish 'blogPosts', () ->
    BlogPosts.find({}, {sort: {date: -1, time: -1}})

Subscribe in the iron-router data method:

# Blog
  @route 'blog',
    path: '/blog/'
    waitOn: ->
      Meteor.subscribe 'blogPosts'
    data: ->
      blogPosts: BlogPosts.find({}, {sort: {date: -1, time: -1}})
like image 868
funkyeah Avatar asked Jan 13 '14 16:01

funkyeah


2 Answers

Perhaps you're missing the sort specifier?

BlogPosts.find({}, {sort: {date: -1, time: -1}})
like image 100
Hubert OG Avatar answered Nov 14 '22 22:11

Hubert OG


I wish I could change the title of this post to "noob question about Cursors and un-reproduced sort failure".

My blog posts were returning incorrectly but that was a code error:

BlogPosts.find({}, {date: -1, time: -1})

instead of:

BlogPosts.find({},sort:{ {date: -1, time: -1}})

then after fixing the code, a redeployment suggested the problem had not resolved itself. I attempted to debug in the console with my misunderstanding of how a cursor should return... e.g. that they are supposed to return unordered was a surprise to me as a newbie (I didn't understand DDP and/or maybe mongo enough... although I don't recall seeing it in the Meteor or mongo docs that I have read).

A database reset and a second redeployment fixed whatever issue had been persisting. So I think I would contribute it user error.

like image 37
funkyeah Avatar answered Nov 14 '22 23:11

funkyeah