【发布时间】: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
}
});
这里可能是什么错误。我该如何解决?
【问题讨论】:
-
当你在匹配中传递日期时,是字符串还是日期实例?
-
@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