我正在使用 Cloud Firestore 为一个项目规划数据库结构,目前正在尝试找出存储数据并将其与用户关联的最佳方法。具体如下:
我的数据库根目录下有一组用户文档,我希望用户能够发布可通过查看显示该用户帖子的用户个人资料页面以及通过查看页面聚合来访问的帖子显示所有用户的帖子。
将用户的帖子存储在用户文档的子集合中将使获取用户的个人资料的帖子变得非常容易,但是是否可以编写一个优雅的查询将每个用户的帖子子集合合并到一个结果集中?
或者更好的方法是在我调用的帖子的根目录中拥有另一个集合,并在每个帖子上有一个包含用户的 UID 或用户文档的非规范化副本的字段?
我正在使用 Firestore 编写一个网络应用程序,需要能够显示“今日热门帖子”,但在考虑不同时区的用户时,我无法正确获取查询。
日期以 UTC 0 形式存储在数据库中,然后通过Moment.js调整为客户端中当前用户的 UTC 偏移量。这工作正常。
添加新帖子时,我使用firebase.firestore.FieldValue.serverTimestamp()将当前服务器时间戳存储在名为 的字段中timestamp,如下所示:
const collectionRef = db.collection('posts');
collectionRef.add({
name: "Test Post",
content: "Blah blah blah",
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
likeCount: 0
});
Run Code Online (Sandbox Code Playgroud)
然后在服务器上,我有一个云函数,它在创建时运行,并向文档添加另一个字段,称为datestampUTC 0 时间戳,但进行了调整,以便时间是一天的开始。该函数如下所示:
exports.updatePostDate = functions.firestore
.document('posts/{postID}')
.onCreate((event) => {
const db = admin.firestore();
const postRef = db.doc('post/'+event.params.postID);
const postData = event.data.data();
const startOfDay = moment(postData.timestamp).startOf('day').toDate();
return postRef.update({
datestamp: startOfDay
});
});
Run Code Online (Sandbox Code Playgroud)
存储时间始终是一天开始的时间戳使我能够编写这样的查询来查找所有帖子并按给定日期的受欢迎程度排序:
const startOfDayUTC = moment.utc().startOf('day').toDate();
const postQuery = db.collection('posts')
.orderBy('likeCount', 'desc')
.orderBy('timestamp', …Run Code Online (Sandbox Code Playgroud) 在我的 Firestore 数据库中,我的集合中有一些文档,如下所示:
{
name: "Item 1",
count: 2,
timestamp: January 29, 2018 at 3:43:12 PM UTC-8
}
Run Code Online (Sandbox Code Playgroud)
我试图查询这个集合,以便文档按count降序排序,并且它们timestamp的日期等于今天的日期。
使用Moment.js,这是我到目前为止的查询:
const startOfToday = moment().startOf('day').toDate();
const endOfToday = moment().endOf('day').toDate();
const query = db.collection('items')
.orderBy('timestamp', 'desc')
.orderBy('count', 'desc')
.where('timestamp', '>=', startOfToday)
.where('timestamp', '<=', endOfToday);
Run Code Online (Sandbox Code Playgroud)
这个查询确实获取了以今天作为timestamp日期的记录,但是它似乎是先对它们进行排序timestamp,如果两个具有相同的timestamp,则它会按 排序count。
显而易见的解决方案是切换orderBy函数的顺序,以便按countthen排序timestamp,但是当我尝试执行此操作时,Firestore 引发了以下错误:
FirebaseError:查询无效。您在字段 'timestamp' 上有一个带有不等式(<、<=、> 或 >=)的 where 过滤器,因此您还必须使用 'timestamp' 作为您的第一个 Query.orderBy(),但您的第一个 Query.orderBy( ) 在字段 'count' …
onSnapshot用于侦听 Cloud Firestore 中大量文档的更改的最高效方法是什么?
(查询最多可能返回约 50-75 个文档,并使用“加载更多”按钮进行分页,该按钮使用query.endBefore)
侦听整个查询的更改更好,还是查询一次并将 onSnapshot 侦听器附加到返回的每个文档更好?
请注意,这些文档不太可能经常更改,但我仍然需要能够在更改时进行监听。