gpt4 book ai didi

node.js - 在mongodb中查询字符串日期格式

转载 作者:可可西里 更新时间:2023-11-01 09:28:42 24 4
gpt4 key购买 nike

我的 mongoDB 集合中有如下数据。我想统计今天的总数或 search_term。

_id:ObjectId("5b7e1d38981cdc1a7c8bb5fc")
search_term:"Baiyoke Boutique"
date:"August 23rd 2018, 9:34:32 am"

_id:ObjectId("5b7e1fa8fa27fd2754080ad9")
search_term:"Baiyoke Boutique"
date:"August 23rd 2018, 9:44:56 am"

_id:ObjectId("5b7e28314d2d0f388c1596cd")
search_term:"Baiyoke Florting Market"
date:"August 23rd 2018, 10:21:21 am"

我试过以下查询。我用过moment。我不是我犯了什么错误。

var start = moment().startOf('day');
var end = moment().endOf('day');

history.find({
date: {
$gte: start,
$lt: end
}
}).count().toArray(
function (e, res) {
if (e) callback(e)
else callback(null, res)
});

最佳答案

您可以在 mongodb 3.6 及更高版本中尝试以下查询

db.collection.find({
"$expr": {
"$gte": [{ "$dateFromString": { "dateString": "$date" }}, start.toDate() ],
"$lt": [{ "$dateFromString": { "dateString": "$date" }}, end.toDate() ]
}
}).count()

或聚合

db.collection.aggregate([
{ "$addFields": {
"date": {
"$dateFromString": {
"dateString": "$date"
}
}
}},
{ "$match": { "date": { "$gte": start.toDate(), "$lt": end.toDate() }}},
{ "$count": "count" }
])

关于node.js - 在mongodb中查询字符串日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com