[英]How to get all posts where current user has not commented yet using firebase realtime database?
My use case is to fetch all posts which is not already commented by current user.我的用例是获取当前用户尚未评论的所有帖子。
I want to achieve this with single query but my current approach is not working.我想通过单个查询来实现这一点,但我目前的方法不起作用。
DB structure:数据库结构:
posts: {
post1:{
content: "Lorem Ipsum"
comments: {
uid1: true,
uid2: true
}
}
}
My query looks like this:我的查询如下所示:
val postsRef = FirebaseDatabase.getInstance().reference.child("posts")
.orderByChild("comments/{uid}")
.equalTo(null)
Above query an index, but my current index is one dynamic value which firebase doesn't allow.上面查询一个索引,但我当前的索引是一个动态值,firebase 不允许。
"posts" : {
"$postId": {
"comments": {
".indexOn" : ".value"
}
}
} }
I'm stuck here, it would be helpful if anyone guide me to achieve this.我被困在这里,如果有人指导我实现这一目标,那将会很有帮助。 Thanks谢谢
Firebase database queries can only check for the presence of a certain value, not for the absence of it. Firebase 数据库查询只能检查某个值是否存在,不能检查是否存在。
So you can either add each user ID to each post and then remove them when they comment, or (more likely) you will have to implement the logic in your application without a database query: just load all posts and remove the ones where you then find that this user already commented.因此,您可以将每个用户 ID 添加到每个帖子,然后在他们评论时删除它们,或者(更有可能)您将不得不在没有数据库查询的情况下在您的应用程序中实现逻辑:只需加载所有帖子并删除您随后的帖子发现这个用户已经评论了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.