GVKun编程网logo

如何使用Android上的whereArrayContains()过滤器查询包含Firestore集合中对象数组的文档?

37

对于想了解如何使用Android上的whereArrayContains()过滤器查询包含Firestore集合中对象数组的文档?的读者,本文将是一篇不可错过的文章,并且为您提供关于AndroidFi

对于想了解如何使用Android上的whereArrayContains()过滤器查询包含Firestore集合中对象数组的文档?的读者,本文将是一篇不可错过的文章,并且为您提供关于Android Firestore查询“错误:找不到符号.whereNotEqualTo”、android – Firebase Firestore – 或查询、android – Firestore – 如何使用路径String创建DocumentReference、android – 可以在FirestoreRecyclerOptions中使用Algolia查询吗?的有价值信息。

本文目录一览:

如何使用Android上的whereArrayContains()过滤器查询包含Firestore集合中对象数组的文档?

如何使用Android上的whereArrayContains()过滤器查询包含Firestore集合中对象数组的文档?

@H_301_0@我在firestore中有一个集合,其中每个文档都包含一系列联系人,我想查询那些联系人的电子邮件ID是特定值的文档.

@H_301_0@我遇到了WhereArrayContains()在https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Query#whereArrayContains(java.lang.String,%20java.lang.Object)处过滤,具有以下描述: –

@H_301_0@

@H_301_0@public Query whereArrayContains (String field, Object value)

Creates and returns a new Query with the additional filter that documents must
contain the specified field, the value must be an array, and that the
array must contain the provided value.

@H_301_0@A Query can have only one whereArrayContains() filter.

@H_301_0@上述方法中的值是否可以指向对象数组中对象内的字段?

@H_301_0@此外,如果方法参数也称为值,则值必须是数组的短语有点混乱.我确信文档意味着该字段应该出现在文档中,其值应该是一个数组,并且该数组应该包含value参数.

解决方法:

@H_301_0@您无法在Firestore查询中查询数组中对象的字段.而是包含数组的查询将与数组中的对象进行比较.

@H_301_0@

@H_301_0@the value must be an array

@H_301_0@这是指您尝试查询的字段的值.更好的措辞就是这样

@H_301_0@

@H_301_0@Creates and returns a new Query with the additional filter that documents must contain the specified field, the value of the specified field must be an array, and that the array must contain the provided value.

@H_301_0@如果您尝试过滤用户ID或类似内容,请考虑向要查询的对象添加第二个数组,然后将ID作为字符串添加到该数组:

@H_301_0@

{
    "name": "test",
    "users": [someUserObject, someOtherUserObject],
    "userIds": ["someId", "someOtherId"]
}
@H_301_0@然后查询该数组:

@H_301_0@

someRef.whereArrayContains("userIds", someId);

Android Firestore查询“错误:找不到符号.whereNotEqualTo”

Android Firestore查询“错误:找不到符号.whereNotEqualTo”

您正在使用Firestore Android SDK的旧版本。直到version 21.7.0的firebase-firestore依赖项才添加whereNotEqualTo。因此,您将需要按照链接的release notes中所述进行升级。回答此问题时的最新版本是22.0.0。

在build.gradle中,确保您至少具有:

implementation "com.google.firebase:firebase-firestore:21.7.0"

android – Firebase Firestore – 或查询

android – Firebase Firestore – 或查询

如何通过一个字段的多个值获取数据?例如,我有包含帖子的数据库,我想查询blogId为1或2的所有帖子,按时间戳排序.

collection("posts").whereEqualTo("blogId", "1")
.whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)

上面的代码不起作用:(

怎么做到这一点?
问候 :)

解决方法:

我找不到任何有关OR条件的OR的能力的文档.但您可以在blogId上重写您的要求,如下所示:

WHERE blogId > 0 AND blogId < 3

试试这段代码:

collection("posts")
.where("blogId", ">", "0")
.where("blogId", "<", "3")
.orderBy("timestamp", Query.Direction.DESCENDING)
.limit(50)

android – Firestore – 如何使用路径String创建DocumentReference

android – Firestore – 如何使用路径String创建DocumentReference

Firebase实时数据库允许使用创建参考

 var reference = db.ref(path);

firestore中是否存在任何方法,以便我可以使用路径String创建文档引用.

如果方法存在,我如何在android中找到路径字符串,然后如何使用该路径在node.js中创建文档引用.

解决方法:

是的,您也可以在Cloud Firestore中实现此目的.所以这些是你的选择:

FirebaseFirestore db = FirebaseFirestore.getInstance();

第一种选择:

DocumentReference userRef = db.collection("company/users");

第二种选择:

DocumentReference userRef = db.document("company/users");

第三种选择:

DocumentReference userRef = db.collection("company").document("users");

android – 可以在FirestoreRecyclerOptions中使用Algolia查询吗?

android – 可以在FirestoreRecyclerOptions中使用Algolia查询吗?

为了让我的用户能够执行更复杂的搜索,我正在与Algolia合作.

com.algolia.search.saas.Query algolia_query =
                    new com.algolia.search.saas.Query(mLastQuery)
                    .setAttributesToRetrieve("content")
                    .setAttributesToRetrieve("category")
                    .setHitsPerPage(10);
            index.searchAsync(algolia_query, new CompletionHandler() {
                @Override
                public void requestCompleted(JSONObject jsonObject, AlgoliaException e) {
                    Log.d("algolia", jsonObject.toString());
                }
            });

我想将此jsonObject转换为与FirestoreRecyclerOptions兼容的东西,以便能够使用FirestoreRecyclerAdapter

谢谢 !

解决方法:

为了更好地理解,我将尝试用一个例子来解释这个.假设我们有一个指向数据库根参考的FirebaseFirestore对象和一个指向名为products的集合的CollectionReference对象.为此,我们可以使用以下两行代码:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");

我们假设我们有一个Cloud Firestore数据库,如下所示:

Firestore-root
     |
     --- products
            |
            --- productIdOne
            |      |
            |      --- productName: "Milk"
            |
            --- productIdTwo
                   |
                   --- productName: "Soy Milk"
            |
            --- productIdThree
                   |
                   --- productName: "Bacon"

要实现此结构,我们使用以下代码将这些产品添加到数据库以及相应的索引:

Map<String, Object> mapOne = new HashMap<>();
mapOne.put("productName", "Milk");
Map<String, Object> mapTwo = new HashMap<>();
mapTwo.put("productName", "Soy Milk");
Map<String, Object> mapThree = new HashMap<>();
mapThree.put("productName", "Bacon");

WriteBatch writeBatch = rootRef.batch();
writeBatch.set(productsRef.document(), mapOne);
writeBatch.set(productsRef.document(), mapTwo);
writeBatch.set(productsRef.document(), mapThree);
writeBatch.commit();

Client client = new Client(YourApplicationID, YourAPIKey);
Index index = client.getIndex("products");

List<JSONObject> productList = new ArrayList<>();
productList.add(new JSONObject(mapOne));
productList.add(new JSONObject(mapTwo));
productList.add(new JSONObject(mapThree));
index.addobjectsAsync(new JSONArray(productList), null);

我们还假设在布局文件,EditText和ListView中有2个视图.

EditText editText = findViewById(R.id.edit_text);
ListView listView = findViewById(R.id.list_view);

要在ListView中实际显示这些产品,我们使用以下代码:

productsRef.get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    List<String> list = new ArrayList<>();
                    for (DocumentSnapshot document : task.getResult()) {
                        list.add(document.getString("productName"));
                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
                    listView.setAdapter(arrayAdapter);
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

为了根据我们在EditText中输入的文本过滤产品,我们需要添加一个TextChangedListener,如下所示:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void afterTextChanged(Editable editable) {
        Query query = new Query(editable.toString())
                .setAttributesToRetrieve("productName")
                .setHitsPerPage(50);
        index.searchAsync(query, new CompletionHandler() {
            @Override
            public void requestCompleted(JSONObject content, AlgoliaException error) {
                try {
                    JSONArray hits = content.getJSONArray("hits");
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < hits.length(); i++) {
                        JSONObject jsonObject = hits.getJSONObject(i);
                        String productName = jsonObject.getString("productName");
                        list.add(productName);
                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
                    listView.setAdapter(arrayAdapter);
                } catch (JSONException e) {
                    e.printstacktrace();
                }
            }
        });
    }
});

因此,为了使其工作,我们创建了一个新的适配器对象.这意味着对于我们在EditText中键入的每个字符,我们创建一个新的适配器,并使用来自数据库的结果填充此适配器.要回答你的问题,为了实现你想要的,你不必更改JSONObject,你已经创建了一个新的查询对象,应该传递给在FirestoreRecyclerOptions对象上调用的setQuery()方法.我刚刚给你一个更简单的例子来理解这个流程.有关详细信息,我还建议您查看此video.

关于如何使用Android上的whereArrayContains()过滤器查询包含Firestore集合中对象数组的文档?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android Firestore查询“错误:找不到符号.whereNotEqualTo”、android – Firebase Firestore – 或查询、android – Firestore – 如何使用路径String创建DocumentReference、android – 可以在FirestoreRecyclerOptions中使用Algolia查询吗?等相关内容,可以在本站寻找。

本文标签: