在 Mongo Rest API "Eve(0.7.4v)" 中,如何使用 POST 请求插入 native Mongodb 字段类型(例如时间戳)
这是使用的 Eve 架构:
DOMAIN = {'middlewaredata': {'timestamp':{'type':'datetime'}}}
MONGO_DBNAME = 'helloworld'
# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
# Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of
# individual items (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
ALLOW_UNKNOWN=True
这是示例代码
import requests,datetime,json
header={'ContentType':'application/json'}
payload=json.dumps({"name":"helloworld","timestamp":datetime.datetime.utcnow().isoformat()}
requests.post("http://locahost:5000/data",headers=header,data=payload)
这会在 Mongodb 集合中创建名为“data”的以下文档
{
"_id": ObjectID("5a26d12a921409307490cd4f"),
"timestamp": "2017-12-04T16:51:25.632389",
"_etag": "a3bc176e34818bd9e4af57a77c11919dbc394c7a",
"_created": ISODate("2017-12-05T17:02:34.000Z")
}
相反,我希望将其插入如下所示 -
{
"_id": ObjectID("5a26d12a921409307490cd4f"),
"timestamp": ISODate("2017-12-04T16:51:25.632389"),
"_etag": "a3bc176e34818bd9e4af57a77c11919dbc394c7a",
"_created": ISODate("2017-12-05T17:02:34.000Z")
}
最佳答案
DATE_FORMAT
设置允许您设置日期时间值的格式。来自 documentation :
A Python date format used to parse and render datetime values. When serving requests, matching JSON strings will be parsed and stored as datetime values. In responses, datetime values will be rendered as JSON strings using this format. Defaults to the RFC1123 (ex RFC 822) standard a, %d %b %Y %H:%M:%S GMT (“Tue, 02 Apr 2013 10:29:13 GMT”).
因此,除非您更改默认设置,否则您需要提供 RFC-1123 格式的字符串,例如“Tue, 02 Apr 2013 10:29:13 GMT”。
关于mongodb - 使用 Eve REST API for MongoDB 时如何存储 native 时间戳类型数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638492/