【问题标题】:Firestore getting the value from different documentsFirestore 从不同的文档中获取价值
【发布时间】:2020-02-01 09:43:36
【问题描述】:
sportsRef.collection("sports").documents [
["bowling" : [
"equipment": "bowling ball, bowling alley, bowling shoes",
"description": "bowl the ball" ]]

["football": [
"equipment": "ball, boots, shin pads, goalie gloves",
"description": "kick the ball in the goal" ]]

["darts": [
"equipment": "darts, dartboard",
"description": "throw the dart at the board" ]]

["boxing": [
"equipment": "boxing gloves, ring",
"description": "punch your contender" ]]

["shot put": [
"equipment": "shot put",
"description": "throw the shot put, looks like a ball" ]]
]
db.collectionGroup("sports").whereField("description", [i don't know what to 
put]).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>(){
@Override
   public void onSuccess(QuerySnapshot queryDocumentSnapshots){
//...
 }
});

有没有一种可能的方法可以查询字段“描述”的所有值?

【问题讨论】:

  • 我不清楚您要完成什么。 “查询字段的所有值”到底是什么意思?您是否阅读过查询 Firestore 的文档? firebase.google.com/docs/firestore/query-data/queries
  • 抱歉用词不当。我想获取“描述”字段中的所有值。
  • 看起来你只有一个值——一个字符串?你想用它做什么?你试过什么?
  • 我想将这些值存储在一个数组中:投球,将球踢进球门,将飞镖扔到板子上,打你的竞争者,投掷铅球,看起来像一个球。
  • 好的,你试过什么?请编辑您的问题以显示您的代码以及您遇到的问题。

标签: android google-cloud-firestore


【解决方案1】:

Firestore 中的查询限制了从集合中检索的文档。由于您要检索所有文档,因此不应使用查询。

db.collectionGroup("sports").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>(){
@Override
   public void onSuccess(QuerySnapshot queryDocumentSnapshots){
//...
 }
});

现在这段代码将从所有sports 集合中获取所有文档。然后在onSuccess 处理程序中,您可以遍历这些文档并从每个文档中获取description 字段。

无法使用 Android SDK 从每个文档中获取 description 字段。此 SDK 始终检索完整的文档,这就是您必须获取 onSuccess 中的字段的原因。

如果您不想传输其他字段的所有数据,请考虑创建一个单独的集合,您只需在其中存储每个文档的 description 值。

【讨论】:

    【解决方案2】:
    db.collection("sports").get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()){
                                spinnerDataList.add(document.getString("Description"));
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2020-09-07
      • 2018-12-06
      • 2019-09-29
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 2020-11-20
      相关资源
      最近更新 更多