在 firebase 中查询
where query in firebase
我是 Firebase 的新手。我在执行查询时遇到问题:
new Firebase("https://blazing-fire-2739.firebaseio.com/users")
.startAt($scope.user.email) //karank@ocodewire.com
.endAt($scope.user.email)
.once('value', function(snap) {
console.log('accounts matching email address', snap.val())
});
这显示我为空。
在 Firebase 中:
users
-JxFNEu31LJ0Efy8PnX5
email: karank@ocodewire.com
firstname:karan
lastname: sofat
请帮忙
.startAt
和 endAt
不接受字符串,它接受 start/end 所在的数字。所以你必须改为
new Firebase("https://blazing-fire-2739.firebaseio.com/users")
.startAt(1)
.endAt(50)
.once('value', function(snap) {
console.log('accounts matching email address', snap.val());
});
或者完全忽略 endAt()
。
在 Query.startAt()
and Query.endAt()
的文档中,在 'Arguments' 下,您会看到第一个参数 value
是:
The value to start [or end] at. The argument type depends on which orderBy*()
function was used in this query. Specify a value that matches the orderBy*()
type. When used in combination with orderByKey()
, the value must be a string.
Querying Data section of the Web Guide 说,
With Firebase database queries, we can selectively retrieve data based on various factors. To construct a query in your database, you start by specifying how you want your data to be ordered using one of the ordering functions: orderByChild()
, orderByKey()
, orderByValue()
, or orderByPriority()
. You can then combine these with five other methods to conduct complex queries: limitToFirst()
, limitToLast()
, startAt()
, endAt()
, and equalTo()
.
- 如果用户 object 有优先级,并且没有为
Query
指定 orderBy,查询将按优先级排序,startAt/endAt 按索引将起作用。您可以使用 .setPriority()
or .setWithPriority()
.
- 如果用户 object 没有设置优先级,并且
Query
没有 orderBy*()
,snapshot.val()
将为空。
对于您的查询,如果用户 object 没有设置优先级,按 email
child 排序将使 startAt
/endAt
像这样工作:
new Firebase("https://blazing-fire-2739.firebaseio.com/users")
.orderByChild('email')
.startAt($scope.user.email)
.endAt($scope.user.email)
.once('value', function(snap) {
console.log('accounts matching email address', snap.val())
});
这是此查询的有效 plnkr example。
然而,正如 Kato 在评论中指出的那样,
"As a general recommendation, orderByChild()
supersedes priorities and should be preferred. Priorities will eventually be phased out in favor of child fields."
由于您的目标是通过她的电子邮件地址找到一个特定的用户,并且由于应该只有一个用户具有给定的 email
child 值,您可以,
- 按
"email"
child键为/users
订购Query
object;
- 查询用户 object,
email
child 为 equalTo(<email>)
。
例如:
var usersRef = new Firebase("https://blazing-fire-2739.firebaseio.com/users");
userRef.orderByChild("email").equalTo($scope.user.email).once("value", function(snapshot) {
if(snapshot.exists()){
console.log(snapshot.val());
}
});
这是第二个查询的有效 plnkr example。
有关详细信息,请查看 Query.equalTo()
and Query.orderByChild()
的文档。
重要 side-note:按 child 键排序时,如果您知道查询的索引是什么,“您可以通过 .indexOn
规则定义它们安全和 Firebase 规则以获得更好的性能。”查看 Indexing Data documentation 了解更多信息。
阅读 Retrieving Data Guide 可能会有帮助。
要在浏览器中检查您的 Firebase 数据并查看 object 的优先级,您可以使用 Vulcan -- 一个很棒的 Google Chrome 扩展。这就是 Vulcan 在 Chrome DevTools 中的样子:
(来源:firebase.com)
希望对您有所帮助!
我是 Firebase 的新手。我在执行查询时遇到问题:
new Firebase("https://blazing-fire-2739.firebaseio.com/users")
.startAt($scope.user.email) //karank@ocodewire.com
.endAt($scope.user.email)
.once('value', function(snap) {
console.log('accounts matching email address', snap.val())
});
这显示我为空。
在 Firebase 中:
users
-JxFNEu31LJ0Efy8PnX5
email: karank@ocodewire.com
firstname:karan
lastname: sofat
请帮忙
.startAt
和 endAt
不接受字符串,它接受 start/end 所在的数字。所以你必须改为
new Firebase("https://blazing-fire-2739.firebaseio.com/users")
.startAt(1)
.endAt(50)
.once('value', function(snap) {
console.log('accounts matching email address', snap.val());
});
或者完全忽略 endAt()
。
在 Query.startAt()
and Query.endAt()
的文档中,在 'Arguments' 下,您会看到第一个参数 value
是:
The value to start [or end] at. The argument type depends on which
orderBy*()
function was used in this query. Specify a value that matches theorderBy*()
type. When used in combination withorderByKey()
, the value must be a string.
Querying Data section of the Web Guide 说,
With Firebase database queries, we can selectively retrieve data based on various factors. To construct a query in your database, you start by specifying how you want your data to be ordered using one of the ordering functions:
orderByChild()
,orderByKey()
,orderByValue()
, ororderByPriority()
. You can then combine these with five other methods to conduct complex queries:limitToFirst()
,limitToLast()
,startAt()
,endAt()
, andequalTo()
.
- 如果用户 object 有优先级,并且没有为
Query
指定 orderBy,查询将按优先级排序,startAt/endAt 按索引将起作用。您可以使用.setPriority()
or.setWithPriority()
. - 如果用户 object 没有设置优先级,并且
Query
没有orderBy*()
,snapshot.val()
将为空。
对于您的查询,如果用户 object 没有设置优先级,按 email
child 排序将使 startAt
/endAt
像这样工作:
new Firebase("https://blazing-fire-2739.firebaseio.com/users")
.orderByChild('email')
.startAt($scope.user.email)
.endAt($scope.user.email)
.once('value', function(snap) {
console.log('accounts matching email address', snap.val())
});
这是此查询的有效 plnkr example。
然而,正如 Kato 在评论中指出的那样,
"As a general recommendation,
orderByChild()
supersedes priorities and should be preferred. Priorities will eventually be phased out in favor of child fields."
由于您的目标是通过她的电子邮件地址找到一个特定的用户,并且由于应该只有一个用户具有给定的 email
child 值,您可以,
- 按
"email"
child键为/users
订购Query
object; - 查询用户 object,
email
child 为equalTo(<email>)
。
例如:
var usersRef = new Firebase("https://blazing-fire-2739.firebaseio.com/users");
userRef.orderByChild("email").equalTo($scope.user.email).once("value", function(snapshot) {
if(snapshot.exists()){
console.log(snapshot.val());
}
});
这是第二个查询的有效 plnkr example。
有关详细信息,请查看 Query.equalTo()
and Query.orderByChild()
的文档。
重要 side-note:按 child 键排序时,如果您知道查询的索引是什么,“您可以通过 .indexOn
规则定义它们安全和 Firebase 规则以获得更好的性能。”查看 Indexing Data documentation 了解更多信息。
阅读 Retrieving Data Guide 可能会有帮助。
要在浏览器中检查您的 Firebase 数据并查看 object 的优先级,您可以使用 Vulcan -- 一个很棒的 Google Chrome 扩展。这就是 Vulcan 在 Chrome DevTools 中的样子:
(来源:firebase.com)
希望对您有所帮助!