Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Date Range in Meteor

Tags:

mongodb

meteor

I doing a search system for a project in Meteor and need to put a field to search between two dates. However, the dates are within the array in MongoDB:

"relatorios" : [
    {
        "mes" : ISODate("2013-11-01T02:00:00Z"),
        "revistas" : "2",
        "brochuras" : "2",
        "livros" : "0",
        "folhetos" : "0",
        "revisitas" : "0",
        "estudos" : "0",
        "horas" : "12"
    },
    {
        "mes" : ISODate("2013-09-01T03:00:00Z"),
        "revistas" : "0",
        "brochuras" : "0",
        "livros" : "0",
        "folhetos" : "0",
        "revisitas" : "0",
        "estudos" : "0",
        "horas" : "12"
    }
]

I have tried to query filtering dates directly by mongo, but could not. I read on some forums about using MapReduce in Meteor. What is the best option? And if possible, how can I do?

like image 696
Murilo Venturoso Avatar asked Jun 30 '14 01:06

Murilo Venturoso


1 Answers

You can use dot notation e.g for two dates between a and b

var start = new Date(450000);
var end = new Date(5450000000000);

CollectionName.find({ 'relatorios.mes' : { $gte : start, $lt: end });

So this would get all documents which have an array which match this field. Remember mongodb extracts documents, so if you have just one array which matches it will give you the whole document.

like image 68
Tarang Avatar answered Sep 18 '22 23:09

Tarang