Meteor search range

I am doing a search engine for a project in Meteor and I need to put a search box between two dates. However, the dates are in an 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 tried to request the filter dates directly 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 this?

+6
source share
3 answers

You can use dot notation, for example, 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 }); 

Thus, it will receive all documents that have an array that matches this field. Remember that mongodb retrieves documents, so if you only have one array that matches it, you will get the whole document.

+11
source

You can use $elemMatch to find a single relatorios array that has a mes value between two dates, and a positional projection operator $ to include only this corresponding element in the output file.

In the shell:

 start = new Date('2013-11-01T01:30:00Z'); end = new Date('2013-11-01T02:30:00Z'); db.test.find( {relatorios: {$elemMatch: {mes: {$gt: start, $lt: end}}}}, {'relatorios.$': 1}) 

If you are not using $elemMatch , then it can match $gt for one element and $lt for another.

Or, if you need to get all relatorios elements in a date range (instead of the first), you can use aggregate :

 db.test.aggregate([ // Get all docs with at least one element in the date range {$match: {relatorios: {$elemMatch: {mes: {$gt: start, $lt: end}}}}}, // Duplicate each doc, once per relatorios element. {$unwind: '$relatorios'}, // Filter those to just the ones in the date range. {$match: {'relatorios.mes': {$gt: start, $lt: end}}} ]) 
0
source
  let date = new Date(); let getFirstDay = new Date(date.getFullYear(), date.getMonth() + 1, 1); let getLastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0); let firstDay = getFirstDay.getFullYear() + '-' + ((''+getFirstDay.getMonth()).length<2 ? '0' : '') + getFirstDay.getMonth() + '-' + ((''+getFirstDay.getDate()).length<2 ? '0' : '') + getFirstDay.getDate()+"T00:00:00.000Z"; let lastDay = getLastDay.getFullYear() + '-' + ((''+getLastDay.getMonth()).length<2 ? '0' : '') + (getLastDay.getMonth()+1) + '-' + ((''+getLastDay.getDate()).length<2 ? '0' : '') + getLastDay.getDate()+"T00:00:00.000Z"; var pipeline = [ { $match: { headofficeId: headofficeId }}, { $match: {'createdDate': {$gt: new Date(firstDay), $lt: new Date(lastDay)}}}, {$group: {_id: "$branchId", total: {$sum: "$actualValue"}}}, { $sort: { total: -1 } } ]; var result = Commissions.aggregate(pipeline); return result; 
0
source

Source: https://habr.com/ru/post/971512/


All Articles