Firestore查询数组

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

Firestore query array

问题

如果我删除 `.whereArrayContainsAny("ItemID", Arrays.asList(onCart))` 这个语句就没有错误它会检索集合中的所有文档但这不是我想要的我只想检索具有与 onCart 列表中的 `ItemID` 值相匹配的文档。`onCart` 是来自前一个活动意图的

> 错误消息java.lang.IllegalArgumentException: 无效数据不支持嵌套数组"

@Override
protected void onStart() {
    super.onStart();
    List<String> onCart = getIncomingIntent();
   
    loadCart(onCart);
}

private void loadCart(List<String> onCart) {
    db.collection("Items")
            .whereArrayContainsAny("ItemID", Arrays.asList(onCart))
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d(TAG, "获取文档时出错", task.getException());
                    }
                }
            });
}

private List<String> getIncomingIntent() {
    if (getIntent().hasExtra("checkOut")) {
        return getIntent().getStringArrayListExtra("checkOut");
    }
    return null;
}}

请注意,这段代码中包含 HTML 编码(如 ")。如果您需要在实际代码中使用这些代码片段,请将 HTML 编码还原为正常的双引号。

英文:

Firestore查询数组
Firestore查询数组

If I remove the .whereArrayContainsAny("ItemID", Arrays.asList(onCart)) the statement, there is no error and it retrieves all the documents in a collection, but that is not what I want, I want to retrieve only the documents with ItemID value that is also in the onCart list onCart is from the intent of the prev activity.

> error msg: java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported"

@Override
protected void onStart() {
super.onStart();
List<String> onCart=getIncomingIntent();
loadCart(onCart);
}
private void loadCart(List<String> onCart) {
db.collection("Items")
.whereArrayContainsAny("ItemID", Arrays.asList(onCart))
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
private List<String> getIncomingIntent() {
if (getIntent().hasExtra("checkOut")) {
return getIntent().getStringArrayListExtra("checkOut");
}
return null;
}}

答案1

得分: 1

"If i remove the .whereArrayContainsAny("ItemID", Arrays.asList(onCart)) statement, there is no error and it retrieves all the documents in a collection

That's the expected behavior since calling .collection("Items") along with .get() will return all documents within Items collection. However, when adding a call to:

.whereArrayContainsAny("ItemID", Arrays.asList(onCart))

It means that you are looking for every document where the ItemID field is an array that contains one of the elements that exist in the onCart array. This is actually no possible because your ItemID property is a String and not an array:

Firestore查询数组

See the double quotes? If you need the functionality that the whereArrayContainsAny() method provides, you should change your ItemID property to be of type array. If you only want to check the values of your onCart array against your String property ItemID, then you should simply use:

.whereIn("ItemID", Arrays.asList(onCart));

This query returns every document where the ItemID field is set to one of the elements that exist in the onCart array."

英文:

> If i remove the .whereArrayContainsAny("ItemID", Arrays.asList(onCart)) statement, there is no error and it retrieves all the documents in a collection

That's the expected behavior since calling .collection("Items") along with .get() will return all documents within Items collection. However, when adding a call to:

.whereArrayContainsAny("ItemID", Arrays.asList(onCart))

It means that you are looking for every document where the ItemID field is an array that contains one of the elements that exist in the onCart array. This is actually no possible because your ItemID property is a String and not an array:

Firestore查询数组

See the double quotes? If you need the functionality that the whereArrayContainsAny() method provides, you should change your ItemID property to be of type array. If you only want to check the values of your onCart array against your String property ItemID, then you should simply use:

.whereIn("ItemID", Arrays.asList(onCart));

This query returns every document where the ItemID field is set to one of the elements that exist in the onCart array.

huangapple
  • 本文由 发表于 2020年7月29日 11:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63145721.html
匿名

发表评论

匿名网友

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

确定