只从集合中检索特定的键 [英] Retrieve only specific keys from collection

查看:101
本文介绍了只从集合中检索特定的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储一组笔记,每个笔记都包含一个标题和一个正文。是否有可能检索没有身体的笔记标题列表?

例如:
$ b $ p $ code
注意:[
ewi4ef36wi:{
title:'Shopping List',
body:'todo'
}
o9kng23inw:{
title:'要做的事',
body:''
}
]



我怎样才能检索标题列表?

解决方案

不。 Firebase的Web / JavaScript API始终会返回您请求的节点下的完整树。

最常用的解决方法是,人们在树中只设置一个二级分支,只保留键。

 注意
1:{body:hello,title:yessir}
2:{body:again,title:title2}
3:{body:there,title:another}
Notes_index
1:true
2:true
3:true

通常被称为索引。你可以在 Notes_index 上加上('child_added'),然后(如果需要)使用 once('value'



索引也常用于使节点可以通过备用密钥访问,例如标题索引:

  Title_index 
another:3
title2:2 $ Firebase是b $ byessir:1

这个最后的结构可能不再需要了,扩展他们的查询API以允许在任何字段上进行排序/过滤。但是对于你的用例,索引仍然是有用的。


I would like to store an array of notes, where each note contains a title and a body. Is it possible to retrieve the list of note titles without the body?

For example:

notes: [ ewi4ef36wi: { title: 'Shopping List', body: 'todo' } o9kng23inw: { title: 'Things To Do', body: '' } ]

How could I just retrieve the list of titles?

解决方案

Nope. Firebase's Web/JavaScript API always returns the full tree under the nodes that you request.

The most common workaround for this is that people set up a secondary branch in the tree where they just keep the keys.

Notes
    1: { "body": "hello", "title": "yessir" }
    2: { "body": "again", "title": "title2" }
    3: { "body": "there", "title": "another" }
Notes_index
    1: true
    2: true
    3: true

This is commonly referred to as an index. You'd on('child_added' on Notes_index and then (if needed) get the content of each note using once('value'.

Indexes are also often used to make nodes accessible by an alternative key. Such as a title index for the above:

Title_index
    "another": 3
    "title2": 2
    "yessir": 1

This last structure might soon not be needed anymore, since Firebase is extending their query API to allow ordering/filtering on any field. But for your use-case an index is still useful.

这篇关于只从集合中检索特定的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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