【问题标题】:Firebase data structure ideasFirebase 数据结构思路
【发布时间】:2017-09-04 13:25:01
【问题描述】:

我在 POS 系统中工作,并且我使用 Firebase 作为后端。我需要制作的报告之一是“销量最多/销量最低的产品”。

我有这样的结构 /sales/:

"1234": {
    "date": 1234567890, // timestamp
    "products": {
        "coca-cola-clasica-355-ml": {
            "quantity": 3,
            "salesPrice": 500,
            "costPrice": 400
        },
        "coca-cola-clasica-600-ml": {
            "quantity": 6,
            "salesPrice": 900,
            "costPrice": 700
        }
    },
    "subtotal": 6400,
    "total": 6400
},
"5678": {
    "date": 1234567890, // timestamp
    "products": {
        "taqueritos-chile-picante": {
            "quantity": 2,
            "salesPrice": 100,
            "costPrice": 80
        },
        "coca-cola-clasica-600-ml": {
            "quantity": 4,
            "salesPrice": 900,
            "costPrice": 700
        }
    },
    "subtotal": 200,
    "total": 200
}

还有 /products/:

{
    "coca-cola-clasica-355-ml": {
        "costPrice": 350,
        "name": "Coca Cola Clasica 355 ml",
        "salesPrice": 500,
        "stock": 99,
        "supplier": "femsa-coca-cola",
        "tax": false,
    },
    "coca-cola-clasica-600-ml": {
        "costPrice": 700,
        "name": "Coca Cola Clasica 600 ml",
        "salesPrice": 900,
        "stock": 99,
        "supplier": "femsa-coca-cola",
        "tax": false,
    },
    "taqueritos-chile-picante": {
        "costPrice": 80,
        "name": "Taqueritos Chile Picante",
        "salesPrice": 100,
        "stock": 500,
        "supplier": "dinant",
        "tax": true
    }
}

因此,如果我现在必须获得“最畅销的产品”,我必须在每次找到产品时遍历所有销售并添加数量,然后订购结果并获得最畅销的产品,这很糟糕。

我有两个想法来解决这个问题:

  1. /products/ 添加属性,例如“soldTimes”,并在每次售出产品时添加数量。
  2. 创建第三个实体调用,例如 soldProducts,并在客户每次购买时计算每种产品的销售数量。

这两种方法中的任何一种都有效吗?我在 Firebase 中遗漏了什么?

感谢您的帮助。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database nosql


    【解决方案1】:

    在 NoSQL 中,您通常会根据应用程序想要使用数据的方式对数据进行建模。因此,如果您的应用需要拥有最畅销产品的列表,您应该考虑将该列表存储在数据库中。

    productLeaderboard: {
        coca-cola-clasica-355-ml: {
            totalQuantity: 3,
            totalSalesPrice: 500,
            totalCost: 400,
            totalProfit: 100
        },
        ...
    }
    

    现在要找到最畅销的产品,您可以这样做:

    ref.child('productLeaderboard').orderByChild('totalQuantity').limitToFirst(3).on(...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多