简体   繁体   English

如何在流星js应用程序中使用createddate字段从集合中获取所有记录

[英]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. 但是在我的作品中,每个日期(createddata字段)大约有50-60条记录。我需要根据选择来获取特定日期的所有记录,然后下载该记录。

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 . 当我尝试调用上述函数时,它返回的transactionRecordundefined How can we do this? 我们应该怎么做?

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: 对于简单的一天,您可以进行$ regex搜索以查找以您要查找的日期开始的字符串:

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) }});
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM