gpt4 book ai didi

mongodb - 将字符串日期转换为 MongoDB 中的日期

转载 作者:行者123 更新时间:2023-12-02 02:31:31 25 4
gpt4 key购买 nike

我有一个集合,其中 resolve_at 字段以字符串格式存储,如下所示:

{"resolved_at": 2020-05-23 05:57:40}

现在我想使用以下查询将其转换为格式为 "%Y-%m-%d" 的日期类型:

db.tickets.aggregate([{"$match":{"botExecResult": "BotSuccess",
"$and":[{"resolved_at":{"$type":"string","$ne":""}}],
}},
{
$project: {
date: {
$dateFromString: {
dateString: '$resolved_at',
format: "%Y-%m-%d"


}}}},
])

执行此操作时,我不断收到此错误:

 Error: command failed: {
"ok" : 0,
"errmsg" : "Error parsing date string '2020-03-17 23:03:29'; 10: Trailing data ' '",
"code" : 241,
"codeName" : "ConversionFailure"
} : aggregate failed
Details:
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12
DBCollection.prototype.aggregate@:1:355
@(shell):1:1

查询结构有问题吗?

最佳答案

以下查询对我有用:

db.tickets.aggregate([{"$match":{"botExecResult": "BotSuccess",
"$and":[{"resolved_at":{"$type":"string","$ne":""}}],
}},
{
$project: {
date: {
$dateToString:{
format: '%Y-%m-%d',
date: {$dateFromString: {dateString: '$resolved_at'}}
}
}
}
},

{
"$match": {"date":{'$lt': "2020-08-05", '$gte': "2020-04-21"}}
}


])

关于mongodb - 将字符串日期转换为 MongoDB 中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64968365/

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