user7350714
user7350714

Reputation: 365

How to get all records from collection using createddate field in meteor js application

I have a sample transactions collection similar to below

[
  {"_id":"123","createddate":"2017-07-24T09:43:31.028Z"},
  {"_id":"124","createddate":"2017-07-25T09:43:31.028Z"},
  {"_id":"125","createddate":"2017-07-26T09:43:31.028Z"},
  {"_id":"123","createddate":"2017-07-28T09:43:31.028Z"},
  {"_id":"126","createddate":"2017-07-27T09:43:31.028Z"}
]

But in my production we have around 50-60 records for each date(createddata field).I need to get all the records for a particular date based on selection then download that.

I used below:

Meteor.methods({
  gettransactionsdata:function(){
    let transactionRecord = transactions.find({"createddate":"2017-07-26"});
    return transactionRecord;
  }
});

When I try calling above function it is returning transactionRecord as undefined. How can we do this?

Upvotes: 0

Views: 33

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

You're storing dates as strings instead of dates which is not recommended for numerous reasons. For a simple day you can do a $regex search to find the strings that begin with the day you're looking for:

transactions.find({ createddate: { $regex: /^2017-07-26/ }});

You could also just pass that date string to your method for a bit more flexibility:

Meteor.methods({
  gettransactionsdata(dateStr) {
    return transactions.find({ createddate: { $regex: new RegExp('^'+dateStr) }});
  }
});

Upvotes: 1

Related Questions