1

I would like to get a list of items with the date of today from my MongoDB. I only get the result I want when I hard code the date: "date":"2017-10-26T07:09:36.417Z in my pushups.js file below.

Here is my code.

Documents in my 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>
Styx
  • 9,863
  • 8
  • 43
  • 53
MrTots
  • 13
  • 4

1 Answers1

0

I think you can use moment js to change the format of your date before saving it to your database.

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

so you can easily filter it.

code.cycling
  • 1,246
  • 2
  • 15
  • 33
  • That won't work and there is a very good reason why. [Read the duplicate](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) and understand. – Neil Lunn Oct 26 '17 at 07:58
  • Hey, thanks for your help. I used moment.js and Here is how I solved it: 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 Oct 26 '17 at 12:12
  • @MrTots Glad i could help. – code.cycling Oct 27 '17 at 00:34