【问题标题】:Getting a list of items from mongodb with the date of today [duplicate]从今天的日期从mongodb获取项目列表[重复]
【发布时间】:2017-10-26 07:29:07
【问题描述】:

我想从我的 MongoDB 中获取包含今天日期的项目列表。只有当我在下面的pushups.js 文件中硬编码日期时,我才会得到我想要的结果:"date":"2017-10-26T07:09:36.417Z

这是我的代码。

我的 MongoDB 中的文档

{
    "_id": {
        "$oid": "59f18ac290531423ecb3cb51"
    },
    "date": "2017-10-26T07:09:36.417Z",
    "count": "25"
}

pushups.js

router.get("/pushupsToday", function (req, res) {
var dateObj = new Date();
console.log(dateObj);

db.pushups.find({"date": dateObj}, function (err, pushups) {
    if (err) {
        res.send(err);
    }
    res.status(200).send({"success": true, "result": pushups});
});

pushups.service.ts

@Injectable()
export class PushupService {

  apiUrl = this.appSettings.getApiURL();
  data;
  removedItemId;
  removedItem;

  constructor(public http: Http, public appSettings: AppSettings) {

  }

  public getTodays() {
    return this.http
               .get(this.apiUrl + 'api/pushupsToday')
               .map(response => response.json().result);
  }

home.ts

getTodays() {
  console.log("getTodays in home.ts");
  this.pushupSeriesToday = this.pushupService.getTodays();
}

home.html

<ion-item *ngFor="let item of pushupSeriesToday | async ">
  <ion-item>{{item.count}} - {{item.date | date}}</ion-item>
</ion-item>

【问题讨论】:

  • “天”实际上是“今天开始”和“明天”之间可能值的“范围”。

标签: angularjs node.js mongodb express


【解决方案1】:

我认为您可以在将日期保存到数据库之前使用 moment js 更改日期格式。

var currentDate = moment(new Date()).format('MM-DD-YYYY');

这样您就可以轻松过滤它。

【讨论】:

  • 那行不通,这是有充分理由的。 Read the duplicate 了解一下。
  • 您好,感谢您的帮助。我使用了 moment.js,这是我解决它的方法: var currentDate = moment(new Date()).format("YYYY-MM-DD"); var currentDateS = currentDate + "T00:00:00:000Z"; var currentDateE = currentDate + "T23:59:59:000Z"; db.pushups.find({"date": {$gte: currentDateS, $lt: currentDateE}},
  • @MrTots 很高兴我能帮上忙。
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多