Node.js获取上周的数据

问题描述 投票:0回答:1

我有像这样的mongoose架构结构:

{
    "_id": {
        "$oid": "59d00283893f4500127271f6"
    },
    "__v": 0,
    "bills": [
        {
            "timestamp": 1513539218509,
            "table_no": 2,
            "user_id": {
                "$oid": "59cf912782b85b0012860ecc"
            },
            "order_id": {
                "$oid": "5a36c5979a54980014f06787"
            },
            "_id": {
                "$oid": "5a36c6929a54980014f0678b"
            },
            "disc_amount": 320,
            "amount": 320
        },
        {
            "timestamp": 1513539299486,
            "table_no": 2,
            "user_id": {
                "$oid": "59cf912782b85b0012860ecc"
            },
            "order_id": {
                "$oid": "5a36c6c99a54980014f0678c"
            },
            "_id": {
                "$oid": "5a36c6e39a54980014f0678f"
            },
            "disc_amount": 160,
            "amount": 160
        },
        {
            "timestamp": 1513540879109,
            "table_no": 2,
            "user_id": {
                "$oid": "59cf912782b85b0012860ecc"
            },
            "order_id": {
                "$oid": "5a36ccfb9a54980014f06790"
            },
            "_id": {
                "$oid": "5a36cd0f9a54980014f06793"
            },
            "disc_amount": 320,
            "amount": 320
        },
        {
            "timestamp": 1513540986507,
            "table_no": 2,
            "user_id": {
                "$oid": "59cf912782b85b0012860ecc"
            },
            "order_id": {
                "$oid": "5a36cd639a54980014f06794"
            },
            "_id": {
                "$oid": "5a36cd7a9a54980014f06797"
            },
            "disc_amount": 240,
            "amount": 320
        }
    ]
}

从子模式账单我想只获取那些只有一周的账单。

我的第一个问题是,它是第一个通过id找到Schema的正确方法,然后得到它的账单并在账单数组中进行处理以仅获取上周的数据。我想这不会有效,因为子模式账单可能太大,因此,获取整个数组不是一个好主意。那么,什么是正确的方法?

第二个问题,是否有更合适的方法来创建模式和查询。(我创建了子模式账单,因此所有与特定id相关的账单都保留在一个地方,因此很容易查询。这有效吗?)。

接受各种建议。提前致谢。

node.js database mongodb mongoose
1个回答
2
投票

您可以在mongoose中为集合创建模型,并按如下方式使用它:

var start = new Date(new Date().getTime() - (7 * 60 * 60 * 24 * 1000));  // get date of last week
  Bill.find({
    'bills.timestamp' : {"$gte": start.getTime()}
  }, {
    // Only include the subdocument(s) that matched the query.
    'bills.$'    : 1
  }, function(err, data){
    console.log(data)
  });

模型:

var mongoose = require('mongoose');

/**
 * Bill Mongo DB model
 * @name billModel
 */
var billModel = function () {

    var billSchema = mongoose.Schema({

    bills:{ type : Array , "default" : [] }

    });


  return mongoose.model('Bill', billSchema);
};

module.exports = new billModel();

对于单个id,可以使用以下代码:

Bill.aggregate([
        {
            $match: {
                '_id': new mongoose.Types.ObjectId('5968dcf965854c7817606014')
            }
        },
        {
          $project: {
             bills: {
                $filter: {
                   input: "$bills",
                   as: "bill",
                   cond: { $gte: [ "$$bill.timestamp", start.getTime() ] }
                }
             }
          }
        },
    ], function (err, result) {
        if (err) {
            console.log(err);
        } else {
            res.json(result);
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.