如何按"_createTime"对消防站文件进行排序?



我正在使用带有Admin SDK的云Firebase函数从我的Firestore集合中获取最新的文档。排序基于timestamp字段。此值在编写文档时明确提供。

获取代码

const fetchedTransaction = (await transactionsColRef.orderBy('timestamp', 'desc')
.limit(1).get()).docs[0]
console.log(fetchedTransaction)
console.log('Transaction created at', fetchedTransaction.createTime.toDate())

console.log语句打印以下输出。查看底部的_createTime

输出

QueryDocumentSnapshot {
_fieldsProto: {
coinsToday: { doubleValue: 0.017, valueType: 'doubleValue' },
timestamp: { timestampValue: [Object], valueType: 'timestampValue' }
},
_ref: DocumentReference {
_firestore: Firestore {
_settings: [Object],
_settingsFrozen: true,
_serializer: [Serializer],
_projectId: 'hidden',
registeredListenersCount: 0,
_lastSuccessfulRequest: 1596692513232,
_backoffSettings: [Object],
_preferTransactions: false,
_clientPool: [ClientPool]
},
_path: QualifiedResourcePath {
segments: [Array],
projectId: 'hidden',
databaseId: '(default)'
},
_converter: {
toFirestore: [Function: toFirestore],
fromFirestore: [Function: fromFirestore]
}
},
_serializer: Serializer {
createReference: [Function (anonymous)],
allowUndefined: false
},
_readTime: Timestamp { _seconds: 1596692513, _nanoseconds: 164886000 },
_createTime: Timestamp { _seconds: 1596692167, _nanoseconds: 68734000 },
_updateTime: Timestamp { _seconds: 1596692167, _nanoseconds: 68734000 }
}
Transaction created at 2020-08-06T05:36:07.069Z

我正在寻找一种通过_createTime订购文档的方法,而不是每次都编写timestamp值。使用orderBy('_createTime')orderBy('createTime')不起作用。

使用当前正在使用的字段是正确的方法。不能对元数据",_createTime"进行排序。根据https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document:";如果您希望能够按创建日期对文档进行排序,则应将时间戳作为字段存储在文档中">

相关内容

  • 没有找到相关文章

最新更新