保存每个月、每周等 MongoDB 逻辑

Save each month, each week etc. MongoDB logic

我很难思考如何从逻辑上解决我的关于从 API 读取并插入 MongoDB 的数据的问题。

假设我有一个名为 "apples" 的字段,由于季节性影响,每个月的金额都会发生变化,我想记录这些变化最多 6 个月前,我该怎么做?显然我无法为过去的几个月保存新值,但展望未来,我该怎么做才能为 11 月保存 11 月的价值,然后为 12 月保存 12 月的价值?

顺便说一句,我想使用 NodeJS。

对不起,如果我不清楚,更难以解释!

亲切的问候, 埃里克

对此数据建模的一种可能方法是为每个产品创建一个文档,该文档将存储其一个月的定价历史记录:

{
    product: "apple",
    amount:[  
     {day: ISODate("2017-11-01T00:00:00.000Z"), price: 24},
     {day: ISODate("2017-11-02T00:00:00.000Z"), price: 20},
     {day: ISODate("2017-11-03T00:00:00.000Z"), price: 19}, 
     {day: ISODate("2017-11-03T00:00:00.000Z"), price: 25} 
],
   quality: "best"
}

听起来您想将事物组合在一起。 mongodb里面有个叫aggregation framework的东西。 您可以用它做很多事情,其中​​之一就是分组。 您可以阅读 $group

中的更多信息

您可以为给定日期分别插入每个苹果(文档)。 例如:

  • 在“2017-11-26T16:00:00Z”中,我们有 6 个苹果,价格为 15
  • 在“2017-11-25T16:00:00Z”中,我们有 4 个苹果,价格为 16
  • 在“2017-10-25T16:00:00Z”中,我们有 9 个苹果,价格为 30

1

假设我们有这三个条目:

/* 1 */
{
    "_id" : ObjectId("5a1adc774d8a2fe38bec83e4"),
    "date" : ISODate("2017-11-26T16:00:00.000Z"),
    "apples" : 6,
    "price" : 15
}

/* 2 */
{
    "_id" : ObjectId("5a1adc924d8a2fe38bec83e8"),
    "date" : ISODate("2017-11-25T16:00:00.000Z"),
    "apples" : 4,
    "price" : 16
}
/* 3 */
{
    "date" : ISODate("2017-10-25T16:00:00.000Z"),
    "apples" : 9,
    "price" : 30
}

现在我们想按月对它们进行分组并对每个月的苹果求和,我们可以执行以下操作:

db.yourCollection.aggregate([
{
    $project: 
    {
       month: { $month: "$date" }, 
       apples: 1, // here we just assign the value of apples. There is no change here
       price: 1 // also just assigning the value to price. Nothing is happening here.
    }
},
{
    $group: // grouping phase
    {
        _id: "$month", // This is what we group by 
        monthApples: {$sum: "$apples"} // here we sum the apples per month
        monthPrice: {$sum: "$price"} // here we sum the price for each month
    }
}
])

$project中我们可以利用date aggregation operators.

上述聚合管道将导致:

/* 1 */
{
    "_id" : 10, // month (October)
    "monthApples" : 9 // sum of apples
    "monthPrice" : 30 // sum of price for month 10
}

/* 2 */
{
    "_id" : 11, // month (November)
    "monthApples" : 10  // sum of apples
    "monthPrice" : 31  // sum of price for month 11
}

2

现在假设我们将苹果类型也保存在数据库中。

/* 1 */
{
    "_id" : ObjectId("5a1adc774d8a2fe38bec83e4"),
    "date" : ISODate("2017-11-26T16:00:00.000Z"),
    "apples" : 6,
    "price" : 15,
    "appleType" : "Goldrush"
}

/* 2 */
{
    "_id" : ObjectId("5a1adc924d8a2fe38bec83e8"),
    "date" : ISODate("2017-11-25T16:00:00.000Z"),
    "apples" : 4,
    "price" : 16,
    "appleType" : "Pink Lady"
}

/* 3 */
{
    "_id" : ObjectId("5a1b1c144d8a2fe38bec8a56"),
    "date" : ISODate("2017-10-25T16:00:00.000Z"),
    "apples" : 9,
    "price" : 30,
    "appleType" : "Pink Lady"
}

我们可以像这样按苹果类型分组。

db.yourCollection.aggregate([
{
    $project: 
    {
       apples: 1, // here we just assign the value of apples. There is no change here
       price: 1, // also just assigning the value to price. Nothing is happening here.
       appleType: 1
    }
},
{
    $group: // grouping phase
    {
        _id: "$appleType", // group by appletype
        monthApples: {$sum: "$apples"}, // here we sum the apples per month
        monthPrice: {$sum: "$price"} // here we sum the price for each month
    }
}
])