Nim*_*hty 43 firebase google-cloud-firestore
我需要帮助来查询具有日期范围的长集合.请参阅以下示例文档.我想使用日期范围查询startTime字段.

Cap*_*apy 75
由于我将'dueDate'字段存储为云防火墙上的时间戳(而不是字符串或数字),我这样做是为了获取2017年到期日的发票文件:
let start = new Date('2017-01-01');
let end = new Date('2018-01-01');
this.afs.collection('invoices', ref => ref
.where('dueDate', '>', start)
.where('dueDate', '<', end)
);
Run Code Online (Sandbox Code Playgroud)
注意: dueDate字段存储在带有Date()对象的firebase中.例如:this.doc.dueDate = new Date('2017-12-25')
小智 23
var startfulldate = admin.firestore.Timestamp.fromDate(new Date(1556062581000));
db.collection('mycollection')
.where('start_time', '<=', startfulldate)
.get()
.then(snapshot => {
var jsonvalue: any[] = [];
snapshot.forEach(docs => {
jsonvalue.push(docs.data())
})
res.send(jsonvalue);
return;
}).catch( error => {
res.status(500).send(error)
});
Run Code Online (Sandbox Code Playgroud)
小智 16
const event = new Date();
const expirationDate = admin.firestore.Timestamp.fromDate(event);
const query = collectionRef.where('startTime', '<=', expirationDate)
Run Code Online (Sandbox Code Playgroud)
小智 13
startTime存储为 时,Timestamp您可以更准确地执行此查询范围(这对于长日期范围或相同日期范围的条件都有好处)。
const start = new Date('2021-01-01T00:00:00.000z');
const end = new Date('2021-03-01T23:59:59.000z');
db.collection('Data').where('startTime', '>=', start).where('startTime', '<=', end).get().then(data => {
//pass your 'data' here
});
Run Code Online (Sandbox Code Playgroud)
我在 Node.js 应用程序中使用了它。希望这有用。
对于最近使用 Firebase Firestore 的每个人,根据您的 Firebase 实现设置(取决于 Firebase 版本)会有所不同。
之前,Firestore 保存Timestamp为Date,但是如文档中所述,它将很快被Timestamp对象替换。请参阅此处的时间戳文档。
您可以通过在代码中添加设置强制 Firebase 使用 Timestamp 对象而不是 Date来强制您的实现,如下例所示:
var firebaseApp = firebase.initializeApp({
apiKey: [APIKEY],
authDomain: [FIREBASEAPPDOMAIN],
projectId: [PROJECTID]
});
var firestore = firebase.firestore();
var settings = { timestampsInSnapshots: true }; // force Timestamp instead of Date
firestore.settings(settings);
Run Code Online (Sandbox Code Playgroud)
小智 5
解决方案是使用 Date.now()。停止使用 Firebase 的时间戳服务,您需要使用以毫秒为单位的时间数值,例如:1514271367000,如果 Firestore 使用 26/12/2017 1:56:07 GMT- 0500 (-05) 将不起作用。查询的一个示例是:
this.fsService.afs.collection('chats/4bY1ZpOr1TPq8bFQ3bjS/finance/123+finance/12345'
, ref => ref.orderBy('hour').startAt(1514184967000).endAt(1514271367000))
.valueChanges().subscribe(data =>{
this.mensajes = data;
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42978 次 |
| 最近记录: |