Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mongodb中是否有更好的设计来制作包裹/钱包系统?_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js mongodb中是否有更好的设计来制作包裹/钱包系统?

Node.js mongodb中是否有更好的设计来制作包裹/钱包系统?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我正在尝试创建一个包含不同用户的软件包/钱包系统 我在mongodb中有4个系列: 业务 const businessSchema = new mongoose.Schema({ name: { type: String, unique: true }, . . . packages: [{ kind: String, price: Number, numberOfitems: Number }] }) const purchasesSchem

我正在尝试创建一个包含不同用户的软件包/钱包系统

我在mongodb中有4个系列:

业务

const businessSchema = new mongoose.Schema({
  name: { type: String, unique: true },
  .
  .
  .
  packages: [{
    kind: String,
    price: Number,
    numberOfitems: Number
  }]

})
const purchasesSchema = new mongoose.Schema({
  userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  businessID: { type: mongoose.Schema.Types.ObjectId, ref: 'Business'},
  purchasedPackage:{
    numberOfItems: Number,
    kind: String,
    price: Number
  }

})
用户

const userSchema = new mongoose.Schema({
  userName: { type: String, unique: true },
  userType: {
    type: String,
    enum: ["USER", "BusinessOwner"],
    default: "USER"
  }
  .
  .
  .
  wallets:[{
    businessID: { type: mongoose.Schema.Types.ObjectId, ref: 'Business'},
    numberOfItems: Number,
    kind: String
  }]

})
const orderSchema = new mongoose.Schema({
  userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  businessID: { type: mongoose.Schema.Types.ObjectId, ref: 'Business'},
  order:{
    numberOfItems: Number,
    kind: String
  }

})
购买

const businessSchema = new mongoose.Schema({
  name: { type: String, unique: true },
  .
  .
  .
  packages: [{
    kind: String,
    price: Number,
    numberOfitems: Number
  }]

})
const purchasesSchema = new mongoose.Schema({
  userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  businessID: { type: mongoose.Schema.Types.ObjectId, ref: 'Business'},
  purchasedPackage:{
    numberOfItems: Number,
    kind: String,
    price: Number
  }

})
赎回订单

const userSchema = new mongoose.Schema({
  userName: { type: String, unique: true },
  userType: {
    type: String,
    enum: ["USER", "BusinessOwner"],
    default: "USER"
  }
  .
  .
  .
  wallets:[{
    businessID: { type: mongoose.Schema.Types.ObjectId, ref: 'Business'},
    numberOfItems: Number,
    kind: String
  }]

})
const orderSchema = new mongoose.Schema({
  userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  businessID: { type: mongoose.Schema.Types.ObjectId, ref: 'Business'},
  order:{
    numberOfItems: Number,
    kind: String
  }

})
业务逻辑的流程如下所示:

  • 业务所有者创建一个包
  • 用户购买一个包并创建一个事务
  • 用户可以兑换物品并创建订单
我正在努力寻找一种更简单的方法来实现这个工作流,同时减少冗余


这是我第一次在Stackoverflow上发帖,请放轻松:)

在我看来,设计基于文档的数据库很大程度上取决于应用程序如何向用户呈现数据,换句话说,有哪些API可用。因此,这取决于系统和用户界面的要求。因此,在建议的设计过程中,我做了很多假设

我将从设计开始,然后解释为什么它可能足以满足系统的假定需求

需要4个集合:

  • 用户
  • 生意
  • 包裹
  • 购买 第一:用户集合。 仅用于存储用户信息,不包括钱包

    第二:商业收藏 仅用于存储业务信息,不包括软件包

    第三:包裹收集 用于存储包,使用非规范化的业务信息进行更好的查询

    const packageSchema = new Schema({
         itemInfo: {}, //The item to be sold in the package: (Name, description, ... etc). Try not to include more than one piece of information in a single field, for example: instead of name being: "ITEM_NAME (The big one)", separate them into two fields: name in one and the size in another. 
         businessInfo: {
            //All data of business needed to show the package in the UI, along with the businessId. 
         },
         count: {}, //Number of items 
         //.... Any other data  needed  for the  package, like price, description, ... etc. 
    });
    
    第四:采购集合 purchase集合表示拥有包的用户

    const purchaseSchema = new Schema({
         user: {}, //Id of the user 
         package: {}, //PackageId along with any denormalized data needed about the package to  display it to the user. 
         status: {type: String, required: true, enum: ["pending", "redeemed", "cancelled"], default: "pending"}, //Status of  the package, for the enum, use a constant variable accessible everywhere in the system, so it's easy to make it consistent.      
    });
    

    设计的基本原理 这是一个包含系统中假定函数的列表,以及使用每个函数的假定频率、对DB的调用数量和调用的简单性

    • 管理员。
      • 新增业务:
        • 频率:低
        • 对DB的调用数:1
        • 简单性:对业务集合的简单插入调用
      • 修改现有业务:
        • 频率:非常低
        • 呼叫数据库的次数:3
        • 简单性:复杂且低效,需要更新:业务、包和采购集合
    • 商业。
      • 添加新包:
        • 频率:中等
        • 对DB的调用数:1
        • 简单性:非常简单,对包集合进行一个原子插入调用。(非规范化的业务数据可以随调用一起发送,因为调用方就是业务本身
      • 修改现有包:
        • 频率:低
        • 对DB的调用次数:1(因为已购买的项目不应更改)
        • 简单性:非常简单,只需对包集合进行一次原子更新调用
    • 顾客。
      • 添加(购买)一个包。
        • 频率:非常高
        • 对DB的调用数:1
        • 简单性:非常简单,一个对集合的原子插入调用
      • 兑换包裹。
        • 频率:非常高
        • 对DB的调用数:1
        • 简单性:非常简单,只需对purchases集合进行一次原子更新调用(更改状态、按status=“pending”和userId进行筛选)
      • 把东西放在钱包里。
        • 频率:非常高
        • 对DB的调用数:1
        • 简单性:非常简单,只需调用一个原子查找集合
      • 通过排序和筛选显示所有可用的包。
        • 频率:非常高
        • 对DB的调用数:1
        • 简单性:非常简单,一个对包集合的原子查找调用,所有数据都在那里
    最后,该设计还可以非常容易地获取有关数据的统计信息,因为以下所有操作只需要对数据库进行一次简单(且高效)的调用:

  • 数一数生意的数量
  • 计算包数(活动、非活动等)
  • 赎回的总金额
  • 每笔生意的总金额
  • 每个用户支付的平均金额
  • 为用户支付的总金额
  • 还有更多