我正在尝试使用 MongoDB Compass 中的导入数据按钮将现在已经变得非常简单的 JSON 导入 MongoDB。
我从这个开始:
{"timestamp":"1728714859000","prio":"0"}
{"timestamp":"1728714859000","prio":"0"}
这导致导入错误:
{"name":"WriteError","message":"'timestamp' must be present and contain a valid BSON UTC datetime value","index":1,"code":2,"op: {"timestamp":"1728714859000","prio":"0","_id":"67397afef3e6b1d4dc9c2f44"}}
通过反复试验,我发现从时间戳中删除引号(如下所示)可以解决该错误。
{timestamp:"1728714859000","prio":"0"}
{timestamp:"1728714859000","prio":"0"}
现在我遇到了一个新的错误,但仅限于 GUI,其中指出:
Failed to import with the following error:
Parser cannot parse input: expected an object key
在查看了一些其他示例之后,我找到了对 a 的引用_id
以及另一种制作时间戳的方法。
{
"_id" : "5d11c815eb946a412ecd677d",
"timestamp" : ISODate("2024-10-10T05:06:44.871Z"),
"name" : "harry"
}
现在我收到错误:
Failed to import with the following error:
Parser cannot parse input: expected a value
根据另一个建议,我尝试使用 Python 插入数据:
import pymongo
import time
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["sklogsdb"]
collection = db["sklogscol"]
record = {
"timestamp": int(time.time() * 1000),
"email": "[email protected]"
}
result = collection.insert_one(record)
这导致了同样的错误:
pymongo.errors.WriteError: 'timestamp' must be present and contain a valid BSON UTC datetime value, full error: {'index': 0, 'code': 2, 'errmsg': "'timestamp' must be present and contain a valid BSON UTC datetime value"}
我在这里做错了什么?