简体   繁体   English

从 Node.js 中的 Firebase 实时数据库中选择特定属性

[英]Select specific properties from Firebase Realtime Database in Node.js

Is there any way to select specific properties from firebase Realtime Database?有没有办法从 firebase 实时数据库中选择特定属性? I know there is a way to retrieve selected properties from firestore but how can get via Realtime database using Node.js我知道有一种方法可以从 firestore 中检索选定的属性,但是如何使用 Node.js 通过实时数据库获取在此处输入图像描述

I want only Notes from everyone nothing else.我只想要每个人的笔记。

Suppose i just want to select Notes from Allergy here is my sample code which i tried but not successes...假设我只想从 Allergy 中选择 Notes,这是我尝试但未成功的示例代码...

admin.database().ref(`vitals/Allergy`).select('Notes').then(result => {//here is my result.....})

But it shows me that select is not a function.......但它告诉我 select 不是一个函数......

Realtime Database doesn't support "projections" like this (neither does Cloud Firestore).实时数据库不支持这样的“投影”(Cloud Firestore 也不支持)。 If you are going to query across multiple child nodes, you are going to get each entire child node that matches the query.如果您要跨多个子节点进行查询,您将获得与查询匹配的每个完整子节点。 Even if you want just one property of each child, you can't avoid the cost of downloading the entire child.即使您只想要每个子项的一个属性,您也无法避免下载整个子项的成本。

If your app is very sensitive to performance on these types of queries, consider duplicating the data such that there is another branch of your database that contains only the "Notes" property, and query that branch alone.如果您的应用程序对这些类型的查询的性能非常敏感,请考虑复制数据,以便数据库的另一个分支仅包含“Notes”属性,并单独查询该分支。 This duplication is common in NoSQL type databases, and is call the "fan out" technique .这种重复在 NoSQL 类型的数据库中很常见,称为“扇出”技术

If you want to save download band then i can't help you further, otherelse:如果您想保存下载带,那么我无法进一步帮助您,否则:

let ArrayOfAllDownloadedNotes = [];
firebase.database().ref('yourRootFolders/vitals/Allergy').once((snapshot)=>{
  ArrayOfAllDownloadedNotes = [...ArrayOfAllDownloadedNotes, snapshot.val().Notes];
});

//Reapeat the firebase formula for every child you want retrieve the Notes from
//You may also use forEach((item)=>{}) function for each folder you want to retrieve the //notes from if you want

console.log(My Array of Notes:',ArrayOfAllDownloadedNotes );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM