如何在NoSQL数据库中处理标签 [英] How to handle tags in a noSQL database

查看:196
本文介绍了如何在NoSQL数据库中处理标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有产品的noSQL数据库。这些产品显然具有某些特征,我可以通过它们的属性将其过滤掉。

I have a noSQL db with products. These products obviously have some characteristics and I can filter the products out by their properties.

这对标签如何起作用?更具体地说,应该如何构造节点,以便可以过滤带有特定标签的项目?

How does this work with tags? More specifically, how should I structure the nodes such that I can filter out items with a specific tag?

Firebase

如果您熟悉Firebase .equalTo(),请继续:我将如何构造我的FB数据库以及如何查询带有特定标记的项目?

Follow up if you are familiar with Firebase .equalTo(): how would I structure my FB db and how would I query out items with a specic tag?

我想到的一件事是为节点具有单独的属性,例如:

One thing I was thinking of is to have seperate properties for the nodes, e.g.:

   $productId
       /tag_1
       /tag_2

但是那么我必须过滤或查询多次。

But then I have to filter or query multiple times. How can this be overcome or are there other ways?

推荐答案

您要放置标记产品下。尝试翻转它。

You're putting the tags underneath the products. Try flipping it.

$tagId
   /$productId

现在,您可以通过标签查找产品而无需查询。

Now you can find a product by a tag without a query.

// https://<my-firebase-app>.firebaseio.com/tags/{tag}/{productId}
var ref = new Firebase('url').child('tags').child($tagId).child($productId);

JSON如下所示:

{
   "tags":{
      "kinda_cool":{
         "product_3":true,
         "product_6":true
      },
      "super_cool":{
         "product_1":true,
         "product_2":true,
         "product_4":true,
         "product_5":true
      }
   }
}

不是真的可以存储任何其他相关信息。但是使用 $ productId 作为关键字,您可以轻松地通过其标签检索产品。

Rather than true you could store any other relevant information. But having the $productId as the key you can easily retrieve a product by it's tag.

这篇关于如何在NoSQL数据库中处理标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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