Firebase - Deep Query orderByChild with equalTo
Firebase - Deep Query orderByChild with equalTo
我试图通过其中的成员来抓取每个项目 - 我已经创建了一个示例数据结构,尽管实际结构要大得多(因为很难重组数据库)。
这是我的查询:
var ref = new Firebase(FBURL + '/chat/meta/project');
var email = 'kerry@email.com';
ref
.orderByChild("email")
.equalTo(email)
.on("child_added", function(snapshot) {
console.log(snapshot.val());
}
);
重要的是要注意,如果我删除它返回所有"项目".equalTo(email)
,而它应该只返回其中的 2 个。
以下是Firebase中的数据:
{
"chat" : {
"meta" : {
"project" : {
"-KAgjWOxjk80HIbNr68M" : {
"name" : "Gman Branding",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:33:25.170Z",
"member" : {
"-KAgkD-2GVESwNwKP3fA" : {
"email" : "abc@gman.com"
},
"-KAgkP3M4nug9Bjn-vY6" : {
"email" : "def@gman.com"
},
"-KAgkP3OF0sUgc9x9p37" : {
"email" : "ghi@gman.com"
},
"-KAgkaMyEOiXft6o-HbO" : {
"email" : "kerry@email.com"
}
}
},
"-KAgl9xlPDU5T4072FgE" : {
"-KAglqH9pxkhgC84_kAl" : {
"name" : "YuDog",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:41:31.172Z"
},
"name" : "billing test 1",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-02-25T23:18:55.626Z",
"dateNotifyUnread" : "2016-01-25T23:23:55.626Z",
"member" : {
"-KAglNsswyk66qUZNrTU" : {
"email" : "kerry@email.com"
}
}
},
"-KAgltmLk2oOYhEDfwRL" : {
"-KAgm1Jt5q53gzLm1GIh" : {
"name" : "YuDog",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:41:31.172Z"
},
"name" : "YuDog",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:41:31.172Z",
"member" : {
"-KAgm1Jvss9AMZa1qDb7" : {
"email" : "joe@yudog.com"
}
}
},
"-KAgluTcE_2dv00XDm1L" : {
"-KAgm6ENmkpDiDG2lqZ4" : {
"name" : "YuDog Landing Page",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:41:31.172Z"
},
"-KAgmBptbeInutRzNinm" : {
"name" : "YuDog Landing Page",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:41:31.172Z"
},
"name" : "YuDog Landing Page",
"url" : "http://localhost:3000/project/abc123fasd123cc/...",
"date" : "2015-10-10T21:41:31.172Z",
"member" : {
"-KAgm6EQcvQg3oP-OnIF" : {
"email" : "joe@yudog.com"
},
"-KAgmBpwoxPYGXS9fLZ9" : {
"email" : "joe@yudog.com"
}
}
}
}
}
}
}
我已经查看了SO上的8-10个其他链接,但没有找到解决此问题的链接。
解决方案是创建符合您需求的数据结构。在这种情况下,您希望根据用户的电子邮件地址查找用户的项目。因此,我们将添加一个包含此映射的节点:
"projects_by_email": {
"kerry@email,com": {
"-KAgjWOxjk80HIbNr68M": true,
"-KAgl9xlPDU5T4072FgE": true
},
"abc@gman,com": {
"-KAgjWOxjk80HIbNr68M": true
}
...
}
这称为非规范化数据,尽管我经常将它们视为倒排索引。我可能会按 uid 保留项目,但结构会是一样的。
使用这样的结构,您可以通过简单的直接查找来获取电子邮件的项目列表:
var ref = new Firebase(FBURL);
var email = 'kerry@email.com';
ref.child('projects_by_email')
.child(email)
.on("child_added", function(snapshot) {
console.log(snapshot.key());
}
);
或者,如果您还想"加入"项目本身:
var ref = new Firebase(FBURL);
var email = 'kerry@email.com';
ref.child('projects_by_email')
.child(email)
.on("child_added", function(snapshot) {
ref.child('project').child(snapshot.key()).once('value', function(projectSnapshot) {
console.log(projectSnapshot.val());
});
}
);
这种类型的非规范化是NoSQL数据建模的正常部分。重复可能会让人觉得浪费,但这也是NoSQL解决方案扩展性如此之好的部分原因:上面的代码都没有要求数据库考虑所有项目/所有用户。这一切都是直接访问正确的节点,这扩展性非常好。因此,我们牺牲了存储空间来获得更高的性能/可扩展性;典型的空间与时间权衡。
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- Modify Javascript with C#
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- Using jQuery with classes from ES6
- Firebase - Deep Query orderByChild with equalTo