Firebase - 获取包含的所有数据 [英] Firebase - Get All Data That Contains

查看:313
本文介绍了Firebase - 获取包含的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个firebase模型,其中每个对象如下所示:

  done:boolean 
|
标签:array
|
text:string

每个对象的标记数组可以包含任意数量的字符串。



如何获得所有匹配标签的对象?例如,查找标记包含email的所有对象。许多更常见的搜索场景,比如按属性搜索(如标签数组所包含的内容),随着API的不断扩展,它们将被放到Firebase中。



同时,自己发展是可能的。根据你的问题,一种方法就是简单地用匹配的记录列表索引标签列表:

  / tags / $ tag / record_ids ... 

然后搜索包含给定标签的记录,您只需对标签列表进行快速查询:

 新的Firebase('URL / tags /'+ tagName).once 'value',函数(snap){
var listOfRecordIds = snap.val();
});

这是一个非常常见的NoSQL口头禅 - 将更多精力放在初始写入中, 。这也是一个常见的方法(和一个大多数SQL数据库在内部使用,在一个更复杂的水平)。

另见弗兰克提到,这将有助于您扩展到更高级的搜索主题。 >

I have a firebase model where each object looks like this:

done: boolean
|
tags: array
|
text: string

Each object's tag array can contain any number of strings.

How do I obtain all objects with a matching tag? For example, find all objects where the tag contains "email".

解决方案

Many of the more common search scenarios, such as searching by attribute (as your tag array would contain) will be baked into Firebase as the API continues to expand.

In the mean time, it's certainly possible to grow your own. One approach, based on your question, would be to simply "index" the list of tags with a list of records that match:

/tags/$tag/record_ids...

Then to search for records containing a given tag, you just do a quick query against the tags list:

new Firebase('URL/tags/'+tagName).once('value', function(snap) {
    var listOfRecordIds = snap.val();
});

This is a pretty common NoSQL mantra--put more effort into the initial write to make reads easy later. It's also a common denormalization approach (and one most SQL database use internally, on a much more sophisticated level).

Also see the post Frank mentioned as that will help you expand into more advanced search topics.

这篇关于Firebase - 获取包含的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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