0

I have following kind of objects stored into mongodb:

 { _id: 5319b78ba96ea4ef5c99dd55,
    name: 'Test',
channel: 'Right one', showed: { _isAMomentObject: true, _i: '12.3.2014 21:45', _f: 'DD.MM.YYYY HH:mm', _l: null, _strict: null, _isUTC: false, _pf: [Object], _a: [Object], _d: Wed Mar 12 2014 21:45:00 GMT-0400 (EDT), _isValid: true, _lang: [Object] } },

I want to fetch last 30 objects in date order. I have tried sorting in this way (and also with showed._d) put it seems to sort things in alphabetic order not by date.

db.open(function(err, db) {
                        var options = {
                            'limit': 30,
                            'sort': ['showed._i','desc']
                        }

                        db.collection('programs', function(err, collection) {
                                collection.find({}, options, function(err, docs) {
                                        docs.toArray(function(err, docs) {
                                                res.json(docs);
                                        });
                                });
                        });
                });
Panu Oksala
  • 3,245
  • 1
  • 18
  • 28
  • Why are you storing the full object, when all you need is the `Date` object? Unfortunately, unless you're willing to convert your collection to use a standardized date format, you won't be able to sort. It looks like there might be a `Date` in your object, but it's got a time zone in it (`"EDT"`), instead of being stored as UTC. – WiredPrairie Mar 17 '14 at 23:57
  • i think that _d has date in ISO format, but you said that EDT is an extra and should not be there? – Panu Oksala Mar 18 '14 at 06:18
  • 1
    Unless all dates are in the same time zone, it won't work. UTC is necessary to sort across time zones uniformly. But, if everything is EDT, you could sort on that property. – WiredPrairie Mar 18 '14 at 10:42

1 Answers1

1

For sorting in descending order, you'll need to use '-1' instead of 'desc'. This is how it's done in the shell:

db.collection.find({}).sort({'showed._i':-1}).limit(30)

Also, the field "showed._i" is stored as a string, rather than a Date. The values will need to converted to Date type for the correct results to be returned by Sorting.

Check this post for an approach to convert string to Date type

Community
  • 1
  • 1
Anand Jayabalan
  • 12,294
  • 5
  • 41
  • 52
  • That returns items which starts with 9.3.2014 21:00, but i have items which are dated as 23.3.2014 18:00 – Panu Oksala Mar 17 '14 at 19:47
  • That's because you are storing the date as a string. And "9.3.2014 21:00" is greater than "23.3.2014 18:00" when sorted by string. You'll need to store the date as ISODate() object for the correct results to be returned. – Anand Jayabalan Mar 17 '14 at 20:30
  • OK, I have some data already in database, so it would required some kind of conversion to get everything run smoothly. – Panu Oksala Mar 18 '14 at 06:19
  • 1
    That's correct. I've edited my post and provided a link for a sample approach to convert String to Date. – Anand Jayabalan Mar 18 '14 at 11:11
  • Thanks got it working. Conversion was surprisingly easy at mongodb :). Pure javascript in conversion just rocks (atleast if you compare against PL/SQL). – Panu Oksala Mar 20 '14 at 19:07
  • Absolutely! No problem. – Anand Jayabalan Mar 20 '14 at 19:28