How to handle tags in a noSQL database

2019-07-10 02:54发布

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

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?

1条回答
Ridiculous、
2楼-- · 2019-07-10 03:16

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);

The JSON would look like this:

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

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.

查看更多
登录 后发表回答