Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Javascript 带有日期的嵌套对象数组属性的MongoDB聚合_Javascript_Mongodb_Date_Mongoose_Aggregation Framework - Fatal编程技术网

Javascript 带有日期的嵌套对象数组属性的MongoDB聚合

Javascript 带有日期的嵌套对象数组属性的MongoDB聚合,javascript,mongodb,date,mongoose,aggregation-framework,Javascript,Mongodb,Date,Mongoose,Aggregation Framework,我有这样的数据 { "_id": ObjectId("52ed12c144aecc4bf004d0b6"), "active": true, "name": "woslo", "specialDays": [ { "_id": ObjectId("5f0576196198a715b0a72c14") "

我有这样的数据

{
  "_id": ObjectId("52ed12c144aecc4bf004d0b6"),
  "active": true,
  "name": "woslo",
 "specialDays": [
  {
  "_id": ObjectId("5f0576196198a715b0a72c14")
  "status": true
  "date": 2020-07-08T04:00:00.000+00:00
  },
  {
  "_id": ObjectId("5f05a3726198a715b0a72c94")
  "status": false
  "date": 2020-07-09T04:00:00.000+00:00
  }
 ]
}
我想使用此查询获取记录

场景是:如果日期出现在specialDays数组中,且状态应为true,或者日期不应出现在specialDays对象的数组中,则获取此记录。 但每次它获取的同一条记录都高于此值,甚至status为false或date出现在数组中。 您能帮我找出答案吗?我在Mongo compass聚合中尝试了很多查询,但仍然无法使用ISODate('2020-07-08')。 非常感谢。
愉快的编码。

问题在于您的
$ne
状况。如果状态为false,则您的
$ne
条件为true。因为它是逻辑OR,所以您将获得输出

怎么样

$ne
条件的另一个原因为true,因为它满足
specialDays
数组中的第一个数组元素

db.collection.aggregate([
  {
    $match: {
      specialDays: {
        $elemMatch: {
          $or: [
            {
              $and: [
                {
                  date: new Date("2020-07-09T04:00:00.000+00:00")
                },
                {
                  status: true
                }
              ]
            },
            {
              $and: [
                {
                  date: {
                    $ne: new Date("2020-07-09T04:00:00.000+00:00")
                  }
                },
                {
                  status: false
                }
              ]
            }
          ]
        }
      }
    }
  }
])

好极了,多亏了你,它解决了我的问题。很高兴,它帮了我的忙。快乐编码@TanzeelSaleem
db.collection.aggregate([
  {
    $match: {
      specialDays: {
        $elemMatch: {
          $or: [
            {
              $and: [
                {
                  date: new Date("2020-07-09T04:00:00.000+00:00")
                },
                {
                  status: true
                }
              ]
            },
            {
              date: {//Changes here
                $gte: new Date("2020-07-09T06:00:00.000+00:00"),
                $lte: new Date("2020-07-09T23:59:59.000+00:00")
              }
            }
          ]
        }
      }
    }
  }
])
db.collection.aggregate([
  {
    $match: {
      specialDays: {
        $elemMatch: {
          $or: [
            {
              $and: [
                {
                  date: new Date("2020-07-09T04:00:00.000+00:00")
                },
                {
                  status: true
                }
              ]
            },
            {
              $and: [
                {
                  date: {
                    $ne: new Date("2020-07-09T04:00:00.000+00:00")
                  }
                },
                {
                  status: false
                }
              ]
            }
          ]
        }
      }
    }
  }
])