How to search for a specific value in Firebase using Golang?

huangapple go评论77阅读模式
英文:

How to search for a specific value in Firebase using Golang?

问题

我正在使用Golang和Firego连接Firebase。我正在尝试搜索一个具有Email: john@gmail.com的管理员。以下是我的数据库结构:

How to search for a specific value in Firebase using Golang?

为此,我尝试了以下代码:

dB.Child("CompanyAdmins").Child("Info").OrderBy("Email").EqualTo("john@gmail.com").Value(&result)

但它没有产生预期的结果。我该如何做?

英文:

I am using Golang and Firego for connecting to Firebase. I am trying to search an admin with Email: john@gmail.com. The following is my Database Structure

How to search for a specific value in Firebase using Golang?

For this I have tried:

  dB.Child("CompanyAdmins").Child("Info").OrderBy("Email").EqualTo("john@gmail.com").Value(&result)

but it does not produce expected result. How can I do this?

答案1

得分: 3

虽然@dev.bmax正确地确定了问题,但解决方案更简单。您可以指定要按顺序排列的属性路径:

dB.Child("CompanyAdmins")
  .OrderBy("Info/Email")
  .EqualTo("john@gmail.com")
  .Value(&result)

更新(2017-02-10):

我刚刚尝试的完整代码:

f := firego.New("https://stackoverflow.firebaseio.com", nil)

var result map[string]interface{}

if err := f.Child("42134844/CompanyAdmins").OrderBy("Info/Email").EqualTo("john@gmail.com").Value(&result); err != nil {
    log.Fatal(err)
}

fmt.Printf("%s\n", result)

这将打印:

map[-K111111:map[Info:map[Email:john@gmail.com]]]

这是我放置数据的确切位置。

更新20170213

这是我定义的索引:

"CompanyAdmins": {
  ".indexOn": "Info/Email"
}

如果这对您不起作用,请提供一个类似完整的代码片段,我可以进行测试。

英文:

While @dev.bmax has the problem identified correctly, the solution is simpler. You can specify the path of a property to order on:

  dB.Child("CompanyAdmins")
    .OrderBy("Info/Email")
    .EqualTo("john@gmail.com")
    .Value(&result)

Update (2017-02-10):

Full code I just tried:

f := firego.New("https://stackoverflow.firebaseio.com", nil)

var result map[string]interface{}

if err := f.Child("42134844/CompanyAdmins").OrderBy("Info/Email").EqualTo("john@gmail.com").Value(&result); err != nil {
	log.Fatal(err)
}

fmt.Printf("%s\n", result)

This prints:

> map[-K111111:map[Info:map[Email:john@gmail.com]]]

Which is the exact place where I put the data.

Update 20170213:

This is the index I have defined:

  "CompanyAdmins": {
    ".indexOn": "Info/Email"
  }

If this doesn't work for you, please provide a similarly complete snippet that I can test.

答案2

得分: 0

你可以直接将信息数据放入CompanyAdmins结构中吗?这样,你的查询就会起作用。

CompanyAdmins
    -id
       -Email: "johndon@gmail.com"
       -Settings:
            - fields
英文:

Can you put Info data directly into CompanyAdmins structure? This way, your query will work.

<pre>
CompanyAdmins
-id
-Email: "johndon@gmail.com"
-Settings:
- fields
</pre>

答案3

得分: 0

您的查询问题在于Info不是CompanyAdmins的直接子级。

您可以在插入值时使用电子邮件作为键,而不是自动生成的键。这样,您就可以直接访问管理员:

dB.Child("CompanyAdmins").Child("john@gmail.com").Child("Info")

否则,您需要重新构建数据库。您的排序字段(电子邮件)应该更高一级,就像Rodrigo Vinicius建议的那样。然后,您的查询将变为:

dB.Child("CompanyAdmins").OrderBy("Email").EqualTo("john@gmail.com")

英文:

The problem with your query, is that Info is not a direct child of CompanyAdmins.

You could use the email as the key instead of an auto-generated one when you insert values. That way, you can access the admin directly:

dB.Child(&quot;CompanyAdmins&quot;).Child(&quot;john@gmail.com&quot;).Child(&quot;Info&quot;)

Otherwise, you need to restructure the database. Your order-by field (email) should be one level higher, like Rodrigo Vinicius suggests. Then, your query will change to:

dB.Child(&quot;CompanyAdmins&quot;).OrderBy(&quot;Email&quot;).EqualTo(&quot;john@gmail.com&quot;)

huangapple
  • 本文由 发表于 2017年2月9日 18:57:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/42134844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定