如何在mongodb中将字符串转换为Date?

问题描述 投票:1回答:1

我在stackoverflow中找到了很多答案,但没有什么能解决我的问题。我有collection,看起来像这样

#DB Name: test 
#Collection Name: firstcollection

{ 
    "_id" : ObjectId("5d8b51e80eeb1c27ce119e52"), 
    "url" : "http://192.168.1.37/second.html", 
    "browser" : "Netscape", 
    "uniqueId" : "dd0c76b1-77c7-445a-8bcd-e58cb239507e", 
    "ip" : " 162.158.166.103", 
    "date" : "25/09/2019 16:37:56"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5d8b51e80eeb1c27ce119e62"), 
    "url" : "http://192.168.1.37/second.html", 
    "browser" : "Netscape", 
    "uniqueId" : "c3a3c5f8-23a8-4626-9b43-b5a95fb02597", 
    "ip" : " 172.69.134.115", 
    "date" : "25/09/2019 16:38:13"
}

[在这里,我想将date形式的String转换为Date格式。我尝试过此代码

> db.firstcollection.aggregate([
...     {$project:{ date:{$dateFromString:{dateString:'$date'}}}}
... ])

但显示错误

assert: command failed: {
    "ok" : 0,
    "errmsg" : "Error parsing date string '25/09/2019 16:37:56'; 0: Unexpected character '2'",
    "code" : 40553,
    "codeName" : "Location40553"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:403:5
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1212:12
@(shell):1:1

我也尝试过此操作,但是它没有显示格式参数

db.firstcollection.aggregate([
    { "$addFields": {
        "date": { 
            "$dateFromString": { 
                "dateString": "$date",
                "format": "%d-%m-%Y %H%M%S" 
            } 
        }
    } }
])

[我发现MongoDB 3.6已从此format中删除了dateFromString中的answer参数,因此我无法指定格式。

我也对MongoDB 3.6的手册感到困惑

在第一个3.6手动链接中,它表示存在format参数First Manual Link在另一个3.6手册链接中,它说没有格式参数Second Manual Link

所以我不知道如何解决此问题。向我建议一些解决方案,以将string中的date转换为MongoDB。我想通过使用指定的from and to date从MongoDB中获取数据。由于它是字符串,因此无法获取正确的数据。为我提供一些解决方案

mongodb mongoose mongodb-query mongoid pymongo
1个回答
0
投票

正如您用pymongo标记的那样,以下是一种快速方法:

from pymongo import MongoClient
import datetime

db = MongoClient()['testdatabase']

db.testcollection.insert_one({
    "url" : "http://192.168.1.37/second.html",
    "browser" : "Netscape",
    "uniqueId" : "dd0c76b1-77c7-445a-8bcd-e58cb239507e",
    "ip" : " 162.158.166.103",
    "date" : "25/09/2019 16:37:56"
})
db.testcollection.insert_one({
    "url" : "http://192.168.1.37/second.html",
    "browser" : "Netscape",
    "uniqueId" : "c3a3c5f8-23a8-4626-9b43-b5a95fb02597",
    "ip" : " 172.69.134.115",
    "date" : "25/09/2019 16:38:13"
})

for item in db.testcollection.find():
    if 'date' in item:
        item['date'] = datetime.datetime.strptime(item['date'], '%d/%m/%Y %H:%M:%S')
        db.testcollection.replace_one({'_id': item['_id']}, item)

结果:

> db.testcollection.find({}, {'_id': 0}).pretty()
{
        "url" : "http://192.168.1.37/second.html",
        "browser" : "Netscape",
        "uniqueId" : "dd0c76b1-77c7-445a-8bcd-e58cb239507e",
        "ip" : " 162.158.166.103",
        "date" : ISODate("2019-09-25T16:37:56Z")
}
{
        "url" : "http://192.168.1.37/second.html",
        "browser" : "Netscape",
        "uniqueId" : "c3a3c5f8-23a8-4626-9b43-b5a95fb02597",
        "ip" : " 172.69.134.115",
        "date" : ISODate("2019-09-25T16:38:13Z")
}
© www.soinside.com 2019 - 2024. All rights reserved.