首页 > 解决方案 > Firebase get data by timestamp

问题描述

I need to filter data using to get specific data for the timestamp match.

For example I need the data where arrivalTime matches with exact date and time fields, timestamp field in database.

I am trying below but it returns no data.

  _arrivalTIme = moment(
            `${todaysDate.format('YYYY/MM/DD')} ${_arrivalTIme}:00`,
          ).toDate();

// _arrivalTIme: Thu May 28 2020 09:00:00 GMT-0600 (Mountain Daylight Time)

return firestore()
  .collection('mainCol')
  .doc(todayDate)
  .collection('subCol')
  .where('arrivalTime', '==', `${_arrivalTIme}`) ... 

my db record looks like this: enter image description here

What am I doing wrong?

标签: node.jsgoogle-cloud-firestore

解决方案


You're comparing a date field in your database with a string from your code. That comparison will never be true.

You'll either need to get a Timestamp value that is exactly the same as the value in the database, or (more commonly) do a range check:

return firestore()
  .collection('mainCol')
  .doc(todayDate)
  .collection('subCol')
  .where('arrivalTime', '>=', new Date('2020-05-28 08:00:00')
  .where('arrivalTime', '<', new Date('2020-05-28 08:01:00')

推荐阅读