Firebase-按列过滤 [英] Firebase - Filter by a column
问题描述
我正在为我的Angular.js应用程序使用Firebase.
I'm using Firebase for my Angular.js application.
我正在寻找与Firebase相同的SQL WHERE
语句.
I'm looking for the equivalent of SQL's WHERE
statement for Firebase.
我在Firebase中存储了一系列电视连续剧,我只想获取那些具有用户输入名称的电视连续剧(在示例searchQuery
中).
I have an array of TV series stored in Firebase, and I want to fetch only these that has the name that the user entered (in the example searchQuery
).
Firebase支持吗?它有这样的东西吗?
Does Firebase support it? Does it have something like this?
var seriesRef = new Firebase('http://{app}.firebaseio.com/series');
var seriesObject = $firebaseObject(seriesRef.query({ name: searchQuery }));
推荐答案
我有一些建议可能对您有帮助:
I have some suggestions that may help here:
- 查看 Firebase查询文档.具体来说,
- Check out the Firebase Query documentation. Specifically,
.orderByChild()
.equalTo()
- 查看此有效的 PLNKR示例 .
- 我在一个公共Firebase实例中复制了您的数据.
- 您要查找的查询是
seriesCollectionRef.orderByChild("name").equalTo(seriesName)
- 如果在输入中输入"
Avatar: The Last Airbender
"并单击查找",则将获得匹配的系列对象.
- Check out this working PLNKR example.
- I replicated your data in one of my public Firebase instances.
- The query that you're looking for is
seriesCollectionRef.orderByChild("name").equalTo(seriesName)
- If you enter '
Avatar: The Last Airbender
' in the input and click "Find", you'll get the matching series object.
- 请参阅扩展AngularFire的文档服务.
- 您可以在不扩展服务的情况下完成同样的事情,请参阅最后的代码片段.
app.factory('SeriesFactory', function(SeriesArrayFactory, fbUrl){ return function(){ var ref = new Firebase(fbUrl+'/series'); return new SeriesArrayFactory(ref); } }); app.factory('SeriesArrayFactory', function($firebaseArray, $q){ return $firebaseArray.$extend({ findSeries:function(seriesName){ var deferred = $q.defer(); // query by 'name' this.$ref().orderByChild("name").equalTo(seriesName).once("value", function(dataSnapshot){ if(dataSnapshot.exists()){ deferred.resolve(dataSnapshot.val()); } else { deferred.reject("Not found."); } }); return deferred.promise; } }); });
控制器
app.controller('HomeController',function($scope, SeriesFactory, fbUrl) { $scope.seriesName = ''; $scope.findSeries = function() { console.log("Finding series with name:'",$scope.seriesName,"'"); var seriesCollection = new SeriesFactory(); seriesCollection.findSeries($scope.seriesName).then(function(data){ console.log("Found",data); $scope.series = data; }).catch(function(error){ console.warn(error); }); }; });
没有扩展服务
如果您不使用工厂,这就是控制器功能的样子:
Without Extended Service
Here is what a controller function would look like if you weren't using the factories:
$scope.findSeriesWithoutFactory = function() { var seriesRef = new Firebase(fbUrl+'/series'); var seriesCollection = $firebaseArray(seriesRef); seriesCollection.$ref().orderByChild("name").equalTo($scope.seriesName).once("value", function(dataSnapshot){ var series = dataSnapshot.val(); if(dataSnapshot.exists()){ console.log("Found", series); $scope.series = series; } else { console.warn("Not found."); } }); };
规则
注意:请注意,您应该在Firebase规则中添加
".indexOn":"name"
,以便查询有效运行,这一点很重要.像这样:Rules
Note: It's important to note that you should add
".indexOn":"name"
to your Firebase rules so that the query runs efficiently. Like so:"yourfirebaseapp": { ".read": "...", ".write": "...", "series": { ".indexOn": "name" } }
这篇关于Firebase-按列过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Check out the Firebase Query documentation. Specifically,