Firebase复杂的查询子句 [英] Firebase complex query clauses

查看:194
本文介绍了Firebase复杂的查询子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Firebase中拥有以下结构:

  {
images:{
first_image:{
url:https://ima.ge/first_image,
uploader:john,
timestamp:1465920841
$ bsecond_image:{
url:https://ima.ge/second_image,
uploader:louis,


peter,
timestamp:1465920990
}
},

票:{$ b $first_image:{
john:up,
louis:down
francis:up
},
second_image:{
john:up,
louis:down
},
third_image:{
francis:up
}






$ b

是否可以查询所有图像 john 没有已经用Firebase投票了?

通过文档阅读,据我了解,这些查询是根本不可能的。所以,如果这是不可能的,我怎么可以尝试重新构造我的数据,以便能够在我给定的例子中这样的查询?



我正在使用新Firebase控制台,目前是Firebase SDK的iOS和Android版本,但我不需要特定的示例,而是想了解如何在Firebase中进行复杂的查询,可以通过重新构建数据或者,如果我误解了文档,通过正常的查询。

在客户端过滤数据的最大问题是,可能有数百万的图像,并检索所有的他们会是真的很糟糕...

解决方案

是的,你可以做到这一点。



下面是一个示例,用于查找没有john voting的节点,它将从问题的结构中返回third_image节点。

  let ref = self.myRootRef.childByAppendingPath(votes)
ref.queryOrderedByChild(jo h $)。queryEqualToValue(nil)
.observeEventType(.Value,withBlock:{snapshot in
for child in snapshot.children {
print(child)
} $ b $记住.Value返回多个节点,所以我们需要迭代,然后用child ... ../.../b


$ 。

请注意,如果这些用户是Firebase用户,则应使用userId而不是用户名称。尽可能解除关联动态数据(例如个人姓名)与其关键字,并参考该关键字。约翰可能想在将来被称为约翰尼。


If I have the following structure in Firebase:

{
  "images": {
    "first_image": {
      "url": "https://ima.ge/first_image",
      "uploader": "john",
      "timestamp": "1465920841"
    },
    "second_image": {
      "url": "https://ima.ge/second_image",
      "uploader": "louis",
      "timestamp": "1465920987"
    },
    "third_image": {
      "url": "https://ima.ge/third_image",
      "uploader": "peter",
      "timestamp": "1465920990"
    }
  },

  "votes": {
    "first_image": {
      "john": "up",
      "louis": "down"
      "francis": "up"
    },
    "second_image": {
      "john": "up",
      "louis": "down"
    },
    "third_image": {
      "francis": "up"
    }
  }
}

Is it possible to query for example all the images which john has not voted for already with Firebase?

I read through the documentation and as I understand it, these kinds of queries are not possible at all. So if this is not possible, how could I try to re-structure my data in order to be able to make queries like that in my given example?

I am using the "new" Firebase console and currently the iOS and Android version of the Firebase SDK but I don't need a specific example, instead I want to understand how it is possible to make complex queries in Firebase, either by re-structuring my data or, if I misunderstood the documentation, through normal queries.

The biggest problem of filtering data on the client side is that there could potentially be millions of images and retrieving all of them would be really bad...

解决方案

Yes, You can do this.

Here's an example to find nodes that do NOT have john voting, which will return the third_image node from the structure in the question.

let ref = self.myRootRef.childByAppendingPath("votes")
ref.queryOrderedByChild("john").queryEqualToValue(nil)
      .observeEventType(.Value, withBlock: { snapshot in
      for child in snapshot.children {
           print(child)
      }
})

Remember .Value returns multiple nodes so we need to iterate over then with for child... to get to each one.

As a side note, if these are Firebase users, you should use the userId instead of the persons name. Disassociate dynamic data (a persons name for example) from it's key whenever possible and refer to the key. John may want to be called Johnny in the future.

这篇关于Firebase复杂的查询子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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