我正在尝试在我的云函数中编写一个复合查询,该复合查询计算时间戳大于当前时间的所有文档。我有两个问题: 1.我当前将时间戳存储为字符串的形式在Firestore中是否可以,还是应该将其保存为Firestore时间戳? 2.在任何一种情况下,如何编写复合查询?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var db = admin.firestore();
exports.welcomeData = functions.https.onCall(async(data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
const uid = context.auth.uid;
var currentDateTime = new Date()
var numberRequestsPosted = 0
var requestsRef = db.collection('requests');
var query = await requestsRef.where('createdBy', '==', uid)
.where('fromDateAndTime','>',currentDateTime).get()
.then(snapshotRequests => {
if (snapshotRequests.empty) {
console.log('WelcomeData: No matching documents.');
}
else {
snapshotRequests.forEach(request => {
numberRequestsPosted++;
})
}
})
return {
numberRequestsPosted: numberRequestsPosted.toString(),
};
});
我的文档具有一个名为fromDateAndTime的字符串类型的文档字段,该字段以以下格式存储:4/24/2019 2:00 AM
答案 0 :(得分:0)
我发现,最简单,最通用的时间戳存储方式是GMT整数版本,原因有以下几点:for example: 1556198939
> int < int
因此,可以节省一些时间和头痛,并将日期时间存储为GMT整数。
此外,请记住要执行var timeStamp = Date.now()/1000
,因为节点的工作时间是毫秒级。