【问题标题】:Create array of array Schema using mongoose for NodeJS使用 mongoose 为 NodeJS 创建数组模式数组
【发布时间】:2016-08-25 10:03:43
【问题描述】:

我想创建一个 DB Schema 来存储数据,如下所示

{
    name : "xyz",
    admin : "admin",
    expense : [ 
                jan: [{expenseObject},{expenseObject}], 
                feb: [[{expenseObject},{expenseObject}]
              ]
}

费用对象

var expenseSchema = new Schema({
    particular : String,
    date : {type : Date, default: Date.now},
    paid_by : String,
    amount : Number
});

谁能帮我创建一个相同的架构。

欢迎任何关于相同概念的更好架构的建议。

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    你可以使用Sub Docs

    var parentSchema = new Schema({
      name: { type: String },
      admin: { type: String },
      expense: [expenseSchema]
    });
    

    或者,如果您需要将费用对象存储在单独的集合中,您可以使用refs,其中 Expense 将是另一个模型的名称

    var parentSchema = new Schema({
      name: { type: String },
      admin: { type: String },
      expense: [{ type: Schema.Types.ObjectId, ref: 'Expense' }],
    });
    

    【讨论】:

      【解决方案2】:
      var expenseSchema = new Schema({
        particular : String,
        date : {type : Date, default: Date.now},
        paid_by : String,
        amount : Number
      });
      
      // your schema
      var mySchema = new Schema({
         name : {type: String, trim: true},
         admin : {type: String, trim: true},
         expense: [expenseSchema]
      });
      

      --- 更新:

      现在有了这个更新,expense 是一个 expenseSchema 的数组,没有任何月份的分类。然后,如果您想获取特定月份的所有费用,您可以简单地进行如下聚合:

      db.users.aggregate(
        [
         // this match is for search the user
         { $match: { name: "<ADMIN NAME>"} },
         // this unwind all expenses of the user selected before
         { $unwind: "$expense" },
         // this project the month number with the expense
         {
           $project: {
            expense: 1, 
            month: {$month: '$expense.date'}
          }
         },
         // this search all the expenses in a particular month (of the user selected before)
         { $match: { month: 8 } },
         // this is optional, it's for group the result by _id of the user
         //(es {_id:.., expenses: [{}, {}, ...]}. Otherwise the result is a list of expense
         {
          $group: {
            _id:"$month",
            expenses: { $addToSet: "$expense"}
          }
        }
       ]);
      

      【讨论】:

      • 我可以通过代码添加 jan、feb 等,而不是在 Schema 中硬编码吗?就像我的意思是说仅在需要时添加月份数组
      • 不。使用此解决方案,您只能对所有月份进行硬编码。您不能定义将来会动态变化的模型(例如添加一个月)。您可以在所有月份更改您的结构或硬编码,如果没有费用,则该数组为空。检查this
      • 有更好的方法吗?我需要为这个文件存储每个月的费用。费用是问题中提到的对象。如果您能提供一些更好的想法来做同样的事情,那就太好了。
      • 当然是的,但我不明白为什么要将月份名称作为数组的键。您可以简单地将expenses 作为费用数组,并且每项费用都有月份(或只是日期)。如果您想要某个特定月份的所有费用,您应该做一个很好的查询。
      • 我觉得最好按月对费用进行分类。但是我是 NoSQL 的新手。所以你认为最好查询月份而不是将其分组为数组?谢谢:)
      猜你喜欢
      • 2019-07-18
      • 2014-12-15
      • 2018-02-07
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2019-07-19
      • 2017-03-24
      相关资源
      最近更新 更多