gpt4 book ai didi

android - 基于属性exist(key)的Firebase查询

转载 作者:行者123 更新时间:2023-11-29 23:26:12 26 4
gpt4 key购买 nike

As I am trying to short my data. So I decided to save userLikeCollection as

questionID = true/false -> 其中 true 表示喜欢,false 表示不喜欢。如果此文档不存在,则表示它既不喜欢也不不喜欢...

ID 由 (userID + questionID) 组成

现在我想查询这个问题是否被用户点赞。有没有办法实现它。

db.collection("userLikeCollection").where(questionID ).exist() 

或者 读取具有属性字符串名称 questionID 的文档

db.collection("userLikeCollection").whereStringProperty(questionID).exist()

where userLikeDocument look like below

(userID + questionID) -> 文档唯一 ID - questionID = true/false//其中 questionID = 每个文档的任何唯一 ID。

编辑问题

UserLikeDocument - 1

  • blahblahQuestionIdOne = true;

UserLikeDocument - 2

  • blahblahQuestionIdTwo = true;

UserLikeDocument - 3

  • blahblahQuestionIdThree = true;

UserLikeDocument - 4

  • blahblahQuestionIdFour = true;

最佳答案

Now I want to query whether this question is like by a user. Is there a way to achieve it. db.collection("userLikeCollection").where(questionID ).exist()

是的。要解决此问题,您应该使用查询和 get() 调用。在代码中它看起来像这样:

Query query = db.collection("userLikeCollection").whereEqualTo("questionID", true);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.exists()) {
Boolean questionID = document.getBoolean("questionID");
}
}
}
}
});

在此代码中,查询将帮助您找到所有 questionID 属性的值为 true 的文档,并使用 document.exists () 将帮助查找该文档是否确实存在。

or read that document which has property string name questionID db.collection("userLikeCollection").whereStringProperty(questionID).exist()

在这种情况下,您还应该使用查询,而不是将 bool 值传递给 whereEqualTo() 方法,您应该传递一个文字字符串:

Query query = db.collection("userLikeCollection").whereEqualTo("questionID", id);

其中 id 包含一个 String 类型的值,它是您要查找的问题的实际 ID。

(userID + questionID) -> Document Unique ID - questionID = true/false //where questionID = any unique id for each document.

这在 Firestore 中实际上是可能的,但前提是属性的名称不同,假设 questionID 包含一个 bool 值,而 id 属性包含一个字符串值。这可以通过链接两个 whereEqualTo() 调用来完成,如下所示:

Query query = db.collection("userLikeCollection")
.whereEqualTo("questionID", true)
.whereEqualTo("id", id);

编辑:

根据您的评论:

whereEqualTo("questionID", true) comes only if it is liked but when it is dislike .It will not retrieved

没错。要解决您的问题,请根据关于 Query limitations 的官方文档:

Cloud Firestore does not support the following types of queries:

  • Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).

换句话说,Firestore 中没有 !=(不等于)运算符。正如他们所说,您可以选择将查询拆分为 greater-thanless-than 查询,然后它将完美地工作。

您还可以创建另一个如下所示的查询:

Query query = db.collection("userLikeCollection").whereEqualTo("questionID", false);

在这种情况下,您得到的问题的 questionID 属性为 false。

编辑2:

根据您编辑的问题,Firestore 无法根据动态 ID 过滤文档。也没有办法使用通配符。属性应具有相同的名称。

关于android - 基于属性exist(key)的Firebase查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com