MongoDB中的$ project字段,日期条件返回为空

时间:2017-02-16 17:39:21

标签: javascript json mongodb mongoose database

如果今天的日期仍在此期间,我需要通过促销活动返回标准平原和平原的数据。

我已经设法做了几乎所有事情,唯一的问题是在$ project中,我的条件是正确的但是返回空。

课程收集:

{
"_id": "58a4fcc8dddbeb0ba0a84ac8",
"updatedAt": "2017-02-16T10:26:37.841Z",
"createdAt": "2017-02-16T01:13:44.936Z",
"name": "Engenharia da computação",
"hours": 5000,
"AreaId": "58a4eec3aa912c63b241698d",
"PriceId": "58a4fbd3dddbeb0ba0a84ac0",
"ModalityId": "584e073edde7042be39b31a2",
"__v": 0,
"isActive": true

}

价格收集:

{
"_id": "58a4fbd3dddbeb0ba0a84ac0",
"description": "Preço de banana",
"__v": 0,
"promotions": [
  {
    "name": "Promoção de natal",
    "rules": "Somente valido no notal",
    "startsAt": "2017-01-16T17:12:26.289Z",
    "expiresAt": "2017-07-16T17:12:26.289Z",
    "_id": "58a4fbd3dddbeb0ba0a84ac1",
    "isActive": false,
    "plains": [
      {
        "parcels": 1,
        "from": 1800,
        "to": 1500,
        "_id": "58a4fbd3dddbeb0ba0a84ac4"
      },
      {
        "parcels": 2,
        "from": 900,
        "to": 800,
        "_id": "58a4fbd3dddbeb0ba0a84ac3"
      },
      {
        "parcels": 3,
        "from": 400,
        "to": 300,
        "_id": "58a4fbd3dddbeb0ba0a84ac2"
      }
    ]
  }
],
"plains": [
  {
    "parcels": 1,
    "from": 2000,
    "to": 1600,
    "_id": "58a4fbd3dddbeb0ba0a84ac7"
  },
  {
    "parcels": 2,
    "from": 1000,
    "to": 999,
    "_id": "58a4fbd3dddbeb0ba0a84ac6"
  },
  {
    "parcels": 3,
    "from": 500,
    "to": 400,
    "_id": "58a4fbd3dddbeb0ba0a84ac5"
  }
]

}

我将从表格中收到数量中的包裹,例如:' 1或2个包裹'。 如果这种情况存在于平原地区。促销活动中的文件,您应该进行搜索,但如果促销活动尚未过期,您应该查看日期,否则您应该查找默认的平原价值。

这是我的疑问:

Course
        .aggregate([
            {
                $match: {
                    _id: ObjectId(req.body.CourseId)
                }
            },
            {
                $lookup: {
                    'from'        : 'prices',
                    'localField'  : 'PriceId',
                    'foreignField': '_id',
                    'as'          : 'price'
                }
            },
            {
                $redact: {
                    $cond: {
                        if  : {
                            $or: [
                                {$eq: ['$parcels', 1]},
                                {$or: '$price'},
                                {$or: '$promotions'},
                                {$or: '$plains'},
                            ]
                        },
                        then: "$$DESCEND",
                        else: "$$PRUNE"
                    }
                }
            },
            {
                $project: {
                    promotions: {
                        $filter: {
                            input: "$price.promotions",
                            as   : "promotion",
                            cond : {$and: [
                                {$lte: ["$$promotion.startsAt", new Date]},
                                {$gte: ["$$promotion.expiresAt", new Date]}
                            ]}
                        }
                    },
                    plains    : "$price.plains"
                }
            }
        ])

回报是:

{
  "_id": "58a4fcc8dddbeb0ba0a84ac8",
  "promotions": [],
  "plains": [
    [
      {
        "parcels": 1,
        "from": 2000,
        "to": 1600,
        "_id": "58a4fbd3dddbeb0ba0a84ac7"
      }
    ]
  ]
}

从理论上讲,一系列促销活动不应该是空的,它是这样的:

[
    {
      "name": "February Promotion",
      "rules": "Valid only in February",
      "startsAt": "2017-01-16T17:12:26.289Z",
      "expiresAt": "2017-07-16T17:12:26.289Z",
      "_id": "58a4fbd3dddbeb0ba0a84ac1",
      "plains": [
        {
          "parcels": 1,
          "from": 1800,
          "to": 1500,
          "_id": "58a4fbd3dddbeb0ba0a84ac4"
        },
        {
          "parcels": 2,
          "from": 900,
          "to": 800,
          "_id": "58a4fbd3dddbeb0ba0a84ac3"
        },
        {
          "parcels": 3,
          "from": 400,
          "to": 300,
          "_id": "58a4fbd3dddbeb0ba0a84ac2"
        }
      ]
    }
  ]

感谢。

0 个答案:

没有答案