gunygoogoo
gunygoogoo

Reputation: 681

MongoDB update with current date and date time

I'm trying to do an update where my unique identifier is a date field (only the date portion matter - not time) but then inserts into an array with the full date (date and time).

The following hardcoded query does what I want:

  db.roomtemps.update(
   {date: ISODate("2015-08-01T00:00:00.000Z")},
    {
    $push: { temps: {val:89, dt: ISODate("2015-08-01T00:04:22.012Z") } }
  },
  {upsert:true}
);

What I'm trying to do is replace the hardcoded with string with the "current date-only" and "current full-date" respectively.

Upvotes: 0

Views: 912

Answers (1)

Love-Kesh
Love-Kesh

Reputation: 812

Use today new date and set times zero as want to compare date only

var todayDate=new Date();

todayDate.setUTCHours(0);
todayDate.setUTCMinutes(0);
todayDate.setUTCSeconds(0);
todayDate.setUTCMilliseconds(0);



console.log(todayDate, todayDate.toISOString());

db.roomtemps.update(
 {date: todayDate},
 {
  $push: { temps: {val:89, dt: ISODate("2015-08-01T00:04:22.012Z") } }
},
{upsert:true}
);

Upvotes: 1

Related Questions