Перевод строки во времени в Mongodb - PullRequest
0 голосов
/ 02 апреля 2020

моя строка похожа на это

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

1 Ответ

0 голосов
/ 04 апреля 2020

я решаю этот вопрос

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)
                                        }
                                    }
                                ] 
                            }
                        ]
                    }]

                 }
            }
        ])
...