【问题标题】:aggregate match returns null with $gte and $lt聚合匹配返回 null 与 $gte 和 $lt
【发布时间】:2020-07-15 18:49:11
【问题描述】:

我正在使用以下代码从 mongodb 中检索数据。不知何故,当我将$match 添加到代码中时,它返回空集。没有它,我的结果很好,所以没有任何其他错误。 $match 中的错误可能是什么。我该如何解决?

这是我的 API 中的代码吗?

postSchemaModel.aggregate([{
            "$geoNear": {
                "near": { "type": "Point", "coordinates": [parseFloat(long), parseFloat(lat), ] },
                "distanceField": "dist.calculated",
                "maxDistance": parseInt(maxDistance),
                "includeLocs": "dist.location",
                "spherical": true
            }
        },
        // mactchString,
        {
            "$match": {
                "createdAt": {
                    "$gte": '2020-07-15 23:54:38.673665',
                    "$lt": '2020-06-15 23:54:38.673665'
                }
            }
        },
        { "$limit": limit },
        { "$skip": startIndex },
        { "$sort": { "createdAt": -1 } },
        {
            "$lookup": {
                "from": userSchemaModel.collection.name,
                "localField": "user_id",
                "foreignField": "_id",
                "as": "user_id"
            }
        },
        {
            "$project": {
                "post_data": 1,
                "likes": 1,
                "commentsCount": 1,
                "post_img": 1,
                "isUserLiked": 1,
                "usersLiked": 1,
                'exp_date': 1,
                "has_img": 1,
                "user_id": {
                    "img": "$user_id.img",
                    "_id": "$user_id._id",
                    "user_name": "$user_id.user_name",
                    "bday": "$user_id.bday",
                    "imagesource": "$user_id.imagesource",
                    "fb_url": "$user_id.fb_url",
                },
                "typology": 1,
                "geometry": 1,
                "category": 1,
                "created": 1,
                "createdAt": 1,
                "updatedAt": 1,
            }
        },
    ]).then(async function(posts) {
        //some code here
        }
    });

以下是来自 mongodb 的示例帖子

这里可能是什么错误。我该如何解决?

【问题讨论】:

  • 当你在匹配中传递日期时,是字符串还是日期实例?
  • @NeerajWadhwa 我尝试使用"$gte": '2020-07-15 23:54:38.673665' 和"$gte": Date.parse('2020-07-15 23:54:38.673665')
  • 尝试传递日期构造函数。
  • @NeerajWadhwa 还是一样!
  • 尝试 new Date().toISOString()

标签: javascript node.js mongodb mongoose mongoose-schema


【解决方案1】:

将日期字段包装在new Date() 中类似于此

"$match": {
  "createdAt": {
    "$gte": new Date('2020-07-15T23:54:38.673Z'),
    "$lt": new Date('2020-06-15T23:54:38.673Z')
   }
}

检查this Q & A。

【讨论】:

  • ISODate 是 MongoDB 的一部分,如果我在这里使用它,它会给我 ISODate 未定义。在这种情况下,我可以使用Date(),但它给了我相同的空结果集
  • 对不起,我的错。尝试以 ISO 日期格式发送日期,例如:2020-07-15T23:54:38.673Z。这应该有效。
  • 试过了!但同样的结果
  • 仍然是string。我们应该告诉mongodb 我猜是date。 new Date('2020-07-15T23:54:38.673Z') 是 the right one。更新了我的答案。
猜你喜欢
  • 2015-08-09
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2019-04-28
  • 2014-10-14
  • 1970-01-01
  • 2020-06-24
  • 2021-09-05
相关资源
最近更新 更多