设计Mongodb模式

时间:2018-12-16 03:04:35

标签: mongodb database-schema mongoose-schema mongoose-populate

我正在使用Mongoose ORM for Stock投资组合设计模式。

预期操作:

  1. 添加/删除/更新交易
  2. 获取投资组合
  3. 获取平均价格,累计回报

我尝试使用Populate设计架构,如下所示:

Portfolio
    id
    name    
    trades --> Trade

Stocks
    id
    name

Trade
    id
    type(buy/sell) 
    date
    quantity
    unitPrice   
    stock --> Stocks

const tradeSchema = new Schema({
  type: { type: String, required: true },
  unitPrice: { type: String, required: true },
  quantity: { type: String, required: true },
  date: { type: String, required: true },
  stock: {
    type: mongoose.Types.ObjectId,
    ref: "Stock"
  }
});
const stockSchema = new Schema({
  name: { type: String, required: true}
});

const portfolioSchema = new Schema({
  name: { type: String, required: true },
  trades: [
    {
      type: Schema.Types.ObjectId,
      ref: "Trade"
    }
  ]
});

预期的投资组合是:

RELIANCE:
  BUY 100@900 10/04/2015 
  SELL 50@1000 10/05/2015 
  BUY 100@850 10/06/2015
HDFCBANK:
  BUY 200@1000 11/05/2015
  SELL 100 @800 12/07/2015
Holdings:
  RELIANCE: 150 @ 875.5 (Avg of all buys) 
  HDFCBANK: 100 @ 1000

如果还有其他更好的方法,请纠正我。

0 个答案:

没有答案
相关问题