admin管理员组文章数量:1332377
I have the following data structure and would like to return the "products" associated with a tag. is it possible to achieve this with the current data structure? If not, how should I structure the data, and what would the query look like to return all "products" that have "Tag 2" for example?
{
"accounts" : {
"-KRPU3FyKT4oPWHYjDrr" : {
"userId" : "1"
},
"-kjnsvakljsndfme;lnmv" : {
"userId" : "2"
}
},
"products" : {
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 1",
"description" : "Description of product 1",
"tags" : [ "Tag 1", "Tag 2", "Tag 3", "etc." ],
"url" : "websiteURL1",
},
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : [ "Tag 1", "Tag 2", "Tag 3", "etc." ],
"url" : "websiteURL2",
}
}
}
My last attempt was this:
firebase.database().ref.child('products').orderByChild('tags').equalTo("Tag 1").once('value', function(products)
Obviously no success.. This however does return all "products" with a tag:
firebase.database().ref.child('products').orderByChild('tags').once('value', function(products)
Thanks in advance!
I have the following data structure and would like to return the "products" associated with a tag. is it possible to achieve this with the current data structure? If not, how should I structure the data, and what would the query look like to return all "products" that have "Tag 2" for example?
{
"accounts" : {
"-KRPU3FyKT4oPWHYjDrr" : {
"userId" : "1"
},
"-kjnsvakljsndfme;lnmv" : {
"userId" : "2"
}
},
"products" : {
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 1",
"description" : "Description of product 1",
"tags" : [ "Tag 1", "Tag 2", "Tag 3", "etc." ],
"url" : "websiteURL1.",
},
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : [ "Tag 1", "Tag 2", "Tag 3", "etc." ],
"url" : "websiteURL2.",
}
}
}
My last attempt was this:
firebase.database().ref.child('products').orderByChild('tags').equalTo("Tag 1").once('value', function(products)
Obviously no success.. This however does return all "products" with a tag:
firebase.database().ref.child('products').orderByChild('tags').once('value', function(products)
Thanks in advance!
Share asked Dec 3, 2016 at 20:55 MacDMacD 5863 gold badges9 silver badges28 bronze badges1 Answer
Reset to default 10If you restructure your data like this:
"products" : {
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 1",
"description" : "Description of product 1",
"tags" : {
"Tag1": true,
"Tag2": true
},
"url" : "websiteURL1."
},
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : {
"Tag3": true,
"Tag4": true
},
"url" : "websiteURL2."
}
}
You can use a deep-path query to obtain the products that have a particular tag:
firebase.database()
.ref('products')
.orderByChild('tags/Tag1')
.equalTo(true)
.once('value', function (products) {
console.log(products.val());
});
Note that this approach will work only if you have a fixed number of known tags, as an index will be required for each tag.
本文标签: javascriptDenormalize Firebase Data For Tag SearchStack Overflow
版权声明:本文标题:javascript - Denormalize Firebase Data For Tag Search - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742295079a2448560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论