使用可选的key参数限制firebase equalTo查询 [英] Limiting the firebase equalTo query with the optional key parameter

查看:304
本文介绍了使用可选的key参数限制firebase equalTo查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种感觉,我可能对这个功能的理解来自SQL背景。文档很少,我还没有找到一个很好的例子或解释。
我正在寻找一个澄清以下从 https://github.com/angular/angularfire2/blob/master/docs/4-querying-lists.md


1按照子,值或优先级排序时,Firebase SDK支持可选的startAt,
endAt和equalTo参数。您可以
使用包含
值和关键字的对象字面值来指定关键参数。例如:startAt:{value:'some-value',key:
'some-key'}。

我想限制我的order-by-timestamp-value查询(orderByChild)使用这个可选的关键参数来限制只有那些子键具有指定值的结果。

  this.angularFireDatabase.list('/ items',{
query:{
orderByChild:'uploadedOn_',
equalTo:{key:' user_uid',value:'fSXX9twkttYqoS67mcRUmPawIep1'}
}
});

// JSON
{
items:{
-KijNTXRKV5rjXEA7NdO:{
uploadedOn_:-1493296081615,
user_uid:fSXX9twkttYqoS67mcRUmPawIep1
},
-KijNoB4MivU4Ts6XgUV:{
uploadedOn_:-1493296169733,
user_uid:fSXX9twkttYqoS67mcRUmPawIep1
} ,
-KijbTMBydwQexbbErlk:{
uploadedOn_:-1493300012491,
user_uid:EAsIjLDFPRZDTq97qGQQyS7sBul1
}
}
}

也许,我试图模仿where SQL子句,而不是以不同的方式构建我的数据。
看起来这个可选的参数只能是项目自己的键。
在我的示例中,它是常规的自动生成的Firebase推送键(例如 - KijNTXRKV5rjXEA7NdO)。
但在这种情况下,我很困惑,将使用 equalTo orderByChild 如果通过不使用可选的参数,结果将始终是单个项目。

解决方案

如果你做 ref.orderBy(prop)。equalTo(value,key) ref.child(key)相同的结果。第二个参数 equalTo()本质上是无用的(不同于 startAt() endAt (),它可以用来切分范围。



另见:


I have a feeling I may have the understanding of this feature wrong coming from the SQL background. The documentation is sparse and I haven't been able to find a good example or an explanation. I am looking for a clarification to the following from https://github.com/angular/angularfire2/blob/master/docs/4-querying-lists.md:

1 The Firebase SDK supports an optional key parameter for startAt, endAt, and equalTo when ordering by child, value, or priority. You can specify the key parameter using an object literal that contains the value and the key. For example: startAt: { value: 'some-value', key: 'some-key' }.

I want to limit my ordered-by-timestamp-value query (orderByChild) using this optional key parameter to restrict the results only to those were a child key has a specified value.

this.angularFireDatabase.list('/items', {
  query: {
    orderByChild: 'uploadedOn_',
    equalTo: { key: 'user_uid', value: 'fSXX9twkttYqoS67mcRUmPawIep1' }
  }
});

// JSON 
{
  "items" : {
    "-KijNTXRKV5rjXEA7NdO" : {
      "uploadedOn_" : -1493296081615,
      "user_uid" : "fSXX9twkttYqoS67mcRUmPawIep1"
    },
    "-KijNoB4MivU4Ts6XgUV" : {
      "uploadedOn_" : -1493296169733,
      "user_uid" : "fSXX9twkttYqoS67mcRUmPawIep1"
    },
    "-KijbTMBydwQexbbErlk" : {
      "uploadedOn_" : -1493300012491,
      "user_uid" : "EAsIjLDFPRZDTq97qGQQyS7sBul1"
    }
  }
}

Perhaps, I'm trying to mimic the where SQL clause when instead I should be structuring my data differently. It seems that this optional key parameter can only be the item's own key. In my example it's the regular auto-generated Firebase push key (e.g. "-KijNTXRKV5rjXEA7NdO"). But in this case I'm confused as to what the use case would be to use equalTo together with orderByChild if the result will always be a single item you could get to by NOT using the optional key parameter.

解决方案

If you do ref.orderBy("prop").equalTo("value", "key") it'll give the same results as ref.child("key"). The second parameter for equalTo() is essentially useless (unlike for startAt() and endAt(), where it can be used to slice a range.

Also see:

这篇关于使用可选的key参数限制firebase equalTo查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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