在Firebase中按数组值搜索 [英] Search by array values in Firebase

查看:43
本文介绍了在Firebase中按数组值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过REST API使用Firebase.我有以下数据库结构:

I use Firebase via REST API. I have following database structure:

{
  "categories" : {
    "Cat1" : {},
    "Cat2" : {},
    "Cat3" : {},
    "Cat4" : {}
  },

  "items" : {
    "item1" : {
      "categories": ["Cat1", "Cat3"]
    },
    "item2" : {
      "categories": ["Cat1", "Cat3"]
    },
    "item3" : {
      "categories": ["Cat1", "Cat2", "Cat3"]
    },
    "item4" : {
      "categories": ["Cat4"]
    }
  }
}

如您所见,类别和项目之间的关系类型为"N<-> N"(一个类别可以包含多个项目,而一个项目可以位于多个类别中).

As you could see we have relations of type "N <-> N" between categories and items (one category could have several items and one item could be in several categories).

现在,我想通过Firebase REST API获取Cat1的所有项目,但是我做不到.

Now I want to get all items of Cat1 via Firebase REST API, but I can not do it.

我们知道数组像整数映射一样存储在Firebase中,如map:

As we know arrays are stored in the Firebase like map with integral indexes:

"categories": {
  "0": "Cat1",
  "1": "Cat2",
  "2": "Cat3",
}

因此,我在实时数据库规则中添加了".indexOn": ["categories/*"]并尝试调用它:
curl 'https://...firebaseio.com/...json?orderBy="categories/*"&equalTo="Cat1"'

So, I added ".indexOn": ["categories/*"] to Realtime Database Rules and tried to call this:
curl 'https://...firebaseio.com/...json?orderBy="categories/*"&equalTo="Cat1"'

但是我只有这个:{ }

因此,我认为正则表达式在Firebase查询中不起作用,因为这样做有效:
实时数据库规则中的".indexOn": ["categories/0"]
curl 'https://...firebaseio.com/...json?orderBy="categories/0"&equalTo="Cat1"'

So, I think that regular expressions do not work in Firebase queries, because this worked:
".indexOn": ["categories/0"] in Realtime Database Rules and
curl 'https://...firebaseio.com/...json?orderBy="categories/0"&equalTo="Cat1"'

当然,我可以将数据库模型更改为以下形式:

Of course, I could change the database model to something like this:

{
  "categories" : {
    "Cat1" : {},
    "Cat2" : {},
    "Cat3" : {},
    "Cat4" : {}
  },

  "items" : {
    "item1" : {},
    "item2" : {},
    "item3" : {},
    "item4" : {}
  },

  "category-items": {
    "Cat1": ["item1", "item2", "item3"],
    "Cat2": ["item3"],
    "Cat3": ["item1", "item2", "item3"]
    "Cat4": ["item4"]
  }
}

并获取category-items并遍历Cat1数组,但是然后我必须调用REST API读取方法太多次(类别中的每个item都需要一个REST API调用).因此,它太昂贵了.

And get the category-items and iterate through the Cat1 array, but then I must to call REST API read method too many times (one REST API call for every item in the category). So, it is too expensive.

那么,有人可以帮助我获取原始数据库模型中某个类别中的所有项目吗?

So, could anybody help me with getting all items in a category in origin database model?

更新
最终模型是:

UPDATE
The final model is:

{
  "categories" : {
    "Cat1" : {},
    "Cat2" : {},
    "Cat3" : {},
    "Cat4" : {}
  },

  "items" : {
    "item1" : {
      "Cat1": true,
      "Cat3": true,
    },
    "item2" : {
      "Cat1": true,
      "Cat3": true,
    },
    "item3" : {
      "Cat1": true,
      "Cat2": true,
      "Cat3": true,
    },
    "item4" : {
      "Cat4": true
    }
  }
}

我也添加了

{
  rules": {
    ...
    "items": {
      ".indexOn": [ "Cat1", "Cat2", "Cat3", "Cat4" ]
    }
  }
}

到实时数据库规则,并且REST API调用为
curl 'https://...firebaseio.com/items.json?orderBy="Cat1"&equalTo=tr‌​ue'

to Realtime Database Rules, and REST API call is
curl 'https://...firebaseio.com/items.json?orderBy="Cat1"&equalTo=tr‌​ue'

感谢弗拉基米尔·加布里埃良

Thanks to Vladimir Gabrielyan

推荐答案

这是我建议的结构.

{
  "categories" : {
    "Cat1" : {
        "items": {
            "item1":{/*Some item info*/},
            "item2":{/*Some item info*/}
        }
    },
    "Cat2" : {
        "items": {
            "item3":{/*Some item info*/}
        }
    },
  },

  "items" : {
    "item1" : {
      "categories": {
          "Cat1": true,
      }
    },
    "item3" : {
      "categories": {
          "Cat2": true, 
          "Cat3": true
      }
    }
  }
}

Cat1/Items/{itemId}和items/{itemId}内部,您需要复制您的item信息,但是我认为可以.

Inside Cat1/Items/{itemId} and items/{itemId} you need to duplicate your item information, but I think that is okay.

另请参阅本文. https://firebase.googleblog.com/2013/04 /denormalizing-your-data-is-normal.html

这篇关于在Firebase中按数组值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆