Firestore查询不等于Uid。

huangapple go评论83阅读模式
英文:

Firestore Query not equal to Uid

问题

抱歉不得不提出这个问题,我已经查阅了无数的链接,但始终没有找到答案。看起来很简单,但实际情况却并非如此。

我知道在Firestore中无法执行不等于查询,因此我的代码如下:

private void getNotEqual() {
    // 其他地方定义了db
    Query query = db.collection("collection")
            .whereLessThan("uid", user.getUid())
            .whereGreaterThan("uid", user.getUid())
            .orderBy("uid");

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            Log.d(TAG, "Size: " + task.getResult().size());
        }
    });
}

但是它仍然没有返回那些不相等的文档。Uid来自Firebase身份验证,并作为文档中的一个字符串字段进行存储。我在这里漏掉了什么吗?

非常感谢您的提前帮助。

Firestore查询不等于Uid。

英文:

I apologize for having to ask this question, I have been researching countless links but have not been able to find it. It seems simple but has been far from it.

I know that you can not do a not equal to query in firestore, thus my code is the following:

private void getNotEqual() {
    // db defined elsewhere
    Query query = db.collection(&quot;collection&quot;)
            .whereLessThan(&quot;uid&quot;, user.getUid())
            .whereGreaterThan(&quot;uid&quot;, user.getUid())
            .orderBy(&quot;uid&quot;);

    query.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
            Log.d(TAG, &quot;Size: &quot; + task.getResult().size());
        }
    });
}

But it is still not returning the documents where it is not equal. The Uid is from Firebase Authentication and is stored as a string as a field in the document. Am I missing something here?

Thank you sincerely in advance.

Firestore查询不等于Uid。

答案1

得分: 2

这个查询将始终不会返回任何文档。它实际上是在请求所有 uid 字段既大于又小于某个值的文档,这是不可能满足的条件。Firestore 查询中的过滤器在逻辑上是 AND 连接的,而不是逻辑上的 OR。Firestore 不支持查询中的逻辑 OR

看起来你想要执行两个单独的查询,然后在客户端中合并结果。一个查询将使用 whereLessThan("uid", user.getUid()),另一个使用 whereGreaterThan("uid", user.getUid())。但老实说,你可能更容易只获取集合中的所有文档,并手动筛选掉不想要的文档。

英文:

This query is always going to return no documents. It is effectively asking for all documents whose uid field is both greater than and less than a certain value, which is an impossible condition to satisfy. The filters in a Firestore query are logically AND'd together. They are not logically OR'd. Firestore does not support logical ORs in queries.

It sounds like you want to perform two separate queries, then combine the results in the client. One query will have whereLessThan(&quot;uid&quot;, user.getUid()) and the other whereGreaterThan(&quot;uid&quot;, user.getUid()). But honestly, it might be easier for you just to get all the documents in the collection, and manually filter out the ones you don't want.

答案2

得分: 0

Firestore v21.7.0引入了不相等查询,因此您现在可以使用:

private void getNotEqual() {
    // 在其他地方定义了db
    Query query = db.collection("collection")
            .whereNotEqualTo("uid", user.getUid())
            .orderBy("uid");

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            Log.d(TAG, "Size: " + task.getResult().size());
        }
    });
}
英文:

Firestore v21.7.0 introduces not equal queries, so you can now use:

private void getNotEqual() {
    // db defined elsewhere
    Query query = db.collection(&quot;collection&quot;)
            .whereNotEqualTo(&quot;uid&quot;, user.getUid())
            .orderBy(&quot;uid&quot;);

    query.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
            Log.d(TAG, &quot;Size: &quot; + task.getResult().size());
        }
    });
}

huangapple
  • 本文由 发表于 2020年7月24日 22:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63076208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定