查询Firebase可避免重复

时间:2017-11-04 15:43:25

标签: firebase firebase-realtime-database nosql

我很困惑,试图找到在用户的结果中返回 MEALS 列表以及相关 OFFERS 的最佳方式,其中tasty = true < / strong>即可。我怎样才能重新构建或查询来实现这个目标?

目前,我查询OFFERS /,其中包含MEAL条目中包含的每个OFFER,其中tasty = true。

由于这个数据库可以进行扩展,如果很多餐都属于这个优惠,那么多次提取相同的优惠是不合理的吗?在momemnt OFFER-2返回给用户两次MEAL-4和MEAL-5 offerArray = [OFFER-1,0FFER-2,OFFER-2],但这可能成为数以千计的不必要的查询,这将是不好的用循环对前端进行排序。我也将获得用户不需要的MEAL-6 JSON,这样会损害性能。有没有办法限制这个,以便每个唯一的报价只返回一次,例如offerArray = [OFFER-1,OFFER-2]。

{
"OFFERS": {
    "OFFER-1": {
        "description": "20% off today!",
        "seller": "SELLER-2",
        "includes": ["MEAL-2"]
    },
    "OFFER-2": {
        "description": "30% off all desserts today!",
        "seller": "SELLER-4",
        "includes": ["MEAL-4", "MEAL-5", "MEAL-6"]
    }
},
"SELLERS": {
    "SELLER-1": {
        "name": "Wholefoods",
        "sells": {
            "MEAL-1": true
        }
    },
    "SELLER-2": {
        "name": "Burger King",
        "offer": ["OFFER-1"],
        "sells": {
            "MEAL-2": true
        }
    },
    "SELLER-3": {
        "name": "UberEats",
        "sells": {
            "MEAL-3": true
        }
    },
    "SELLER-4": {
        "name": "Deliveroo",
        "offer": ["OFFER-2"],
        "sells": {
            "MEAL-4": true,
            "MEAL-5": true
        }
    }
},
"MEALS": {
    "MEAL-1": {
        "price": 5.95,
        "seller": "SELLER-1",
        "tasty": true
    },
    "MEAL-2": {
        "offer": ["OFFER-1"],
        "price": 4.8,
        "seller": "SELLER-2",
        "tasty": true
    },
    "MEAL-3": {
        "price": 6.0,
        "seller": "SELLER-3",
        "tasty": true
    },
    "MEAL-4": {
        "offer": ["OFFER-2"],
        "price": 5.25,
        "seller": "SELLER-4",
        "tasty": true
    },
    "MEAL-5": {
        "offer": ["OFFER-2"],
        "price": 4.6,
        "seller": "SELLER-4",
        "tasty": true
    },
    "MEAL-6": {
        "offer": ["OFFER-2"],
        "price": 5.9,
        "seller": "SELLER-4"
    },
    "MEAL-7": {
        "price": 4.26,
        "seller": "SELLER-4"
    }
}

}

非常感谢有关nosql结构的任何建议。

0 个答案:

没有答案