Mongodb query/data filtering


I have a mongodb collection with following data:

    1. Unique ID:
  • “4A3wWEWuLFhpWNxuq”

    1. Location A:
  • “New York”

    1. Location B:
  • “London”

  • . 4 Car:
  • . “593LFV”

  • . 5. createdAt:
  • Mon Feb 13 2017 22:20:31 GMT+0200 (FLE Standard Time)

  • 6 date
  • “13.02.2017”

  • 7 dist:
  • “8”

  • 8 userId:
  • “tH8TriecENs4PMF8E”

So, I’m using the user ID to filter the content to user that is subscribed, however, how do I for example filter the data.
Let’s say I have 3 trips, 2 of them are in February (date XX.02.XXXX), how do I achieve that?
Also if anyone knows, some good meteor beginner level tutorials, I have coded in C# and JAVA, but never tried making a web application. (Yes, I know, there’s ASP.NET, but no… Meteor seems the best for me).

It’s not usually recommended to store dates as strings. However, if you want to do that and make your life easier for filtering/sorting etc, I suggest using ISO8601: it’s a standard and is less ambiguous than “”. This allows date representations such as “2017-02-13” and “20170213”, both of which are naturally ordered using left-to-right string comparisons.

Your filter for February could then look something like:

MyCollection.find({ date: { $and: [ { $gte: '2017-02' }, { $lt: '2017-03' } ] } });

That’s equivalent to: date >= '2017-02' && date < '2017-03'

One other thing you should do is to add an index to date, or performance can take a big hit on large collections.

1 Like

Thank you very much. I’ve changed it to have years, months and days separately, not as one string.

1 Like