javascript - 字符串在MongoDB中进行时间转换
问题描述
我的字符串是这样的
shedule_time = {
start_time : "13:00",
end_time : "14:10"
}
现在更改为 mongodb 中的时间
i used dateFromString but its not working
聚合:
db.getCollection('appointments').aggregate( [
{ "$addFields": { "date": { "$dateFromString": { "dateString": "$schedule_time.from", "format":"%H%M" } } } }
] )
错误:
error assert: command failed: { "ok" : 0, "errmsg" : "Unrecognized argument to $dateFromString: format", "code" : 40541, "codeName" : "Location40541" } : aggregate failed _getErrorWithCode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandWorked@src/mongo/shell/assert.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1
解决方案
我解决了这个问题
db.collections.aggregate([
{
"$project": {
"doctor":1,
"dates":{ $dateToString: { format: "%Y-%m-%d", date: "$schedule_date" } },
"from":{
"$dateFromString": {
"timezone": "+05:30",
"dateString": {"$concat": [{ $dateToString: { format: "%Y-%m-%d", date: "$schedule_date" } },
{ "$concat": ["$schedule_time.from", ":00"] } ] },
}
},
"to":{
"$dateFromString": {
"timezone": "+05:30",
"dateString": {"$concat": [{ $dateToString: { format: "%Y-%m-%d", date: "$schedule_date" } },
{ "$concat": ["$schedule_time.to", ":00"] } ] },
}
}
},
},{
$match:{
$and: [{
"doctor": ObjectId(obj.doctor)
},
{
$or: [
{
$and: [
{
from: {
$gte: new Date(from)
}
},
{
to: {
$lte: new Date(to)
}
}
]
},
{
$and: [
{
from: {
$gte: new Date(from)
}
},
{
from: {
$lte: new Date(from)
}
}
]
}, {
$and:[
{
to: {
$gte: new Date(to)
}
},
{
to: {
$lte: new Date(to)
}
}
]
}
]
}]
}
}
])
推荐阅读
- android - 如何使用套接字从另一个应用程序获取 PWA 中的数据?
- javascript - 为什么我的表格标题在过滤表格数据时消失了?
- unity3d - 如何根据绝对位姿和相对位姿确定当前位姿?
- json - JSON 从平面键值对到嵌套对象
- java - 有没有办法在蓝牙条码扫描仪输入之前清除edittext
- javascript - 在本地 Storage js 中存储 2 个不同的数组
- css - 如何在本地导入 npm 包 css?
- javascript - 如何在现有文档 couchdb 中发布元素
- logging - Loki Grafana 读取或写入模式不起作用
- python - 将数据从我的数据库中检索给用户时遇到问题