Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb mongo可以获取月份和年份范围内的数据_Mongodb_Date_Range - Fatal编程技术网

Mongodb mongo可以获取月份和年份范围内的数据

Mongodb mongo可以获取月份和年份范围内的数据,mongodb,date,range,Mongodb,Date,Range,我想得到一个月和一年范围内的数据。我的文件是这样的 { "month": 12, "year": 2019, "accounts": [ 1094, 1093, 1087, 1081, 1080 ] } { "month": 1, "year": 2020, "accounts": [ 1094, 1093,

我想得到一个月和一年范围内的数据。我的文件是这样的

 {
    "month": 12,
    "year": 2019,
    "accounts": [
        1094,
        1093,
        1087,
        1081,
        1080
    ]
}

{
    "month": 1,
    "year": 2020,
    "accounts": [
        1094,
        1093,
        1087,
        1081,
        1080
    ]
}
我尝试了以下代码

$condition = array('year' => array('$gte' => intval($start_year), '$lte' => intval($end_year)), 'month' => array('$gte' => intval($start_month), '$lte' => intval($end_month)));
$pipeline = array(
                    array('$match' => $condition),
                    array('$sort' => array('month' => 1)),
                    array('$project' => $project)
                );
$res = $mlab_con->$collection->aggregate($pipeline);

但这是不对的。 例如

startmonth=5

startyear=2019

月末=2

年底=2020年


在月计算中,大于5小于2将不会返回任何结果。如何更正此问题?

您可以尝试下面的查询

db.collection.find({
  $or: [
    {
      $and: [
        { year: { $gte: 2019 } },
        { month: { $gte: 5 } }
      ]
    },
    {
      $and: [
        { year: { $lte: 2020 } },
        { month: { $lte: 2 } }
      ]
    }
  ]
})

您可以尝试下面的查询

db.collection.find({
  $or: [
    {
      $and: [
        { year: { $gte: 2019 } },
        { month: { $gte: 5 } }
      ]
    },
    {
      $and: [
        { year: { $lte: 2020 } },
        { month: { $lte: 2 } }
      ]
    }
  ]
})

我认为这会奏效。。我想这会管用的。。