mongodb - 使用 Eve REST API for MongoDB 时如何存储 native 时间戳类型数据?

标签 mongodb rest post timestamp eve

在 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/

相关文章:

php - 使用 FOSRestBundle 将 POST 请求主体序列化为数组

php - Autoselect 和 jQuery post 不能一起工作

asp.net - 如何在不重定向的情况下发帖?

post - 使用 Elm 从 POST 请求下载文件

mongodb - 负整数 Date 值在 MongoDB 中意味着什么?

javascript - Meteor.user() 没有在客户端显示自定义字段

javascript - 不断获取 HTTP REQUEST 的 HTTP 响应代码 "1"

rest - 使用Teamcity REST API获取构建日志

node.js - 如何通过 REST API 微服务(使用 Express 构建)将 MongoDB 更改流与 Node js 一起使用

mongodb - 如何使用 golang 和 mongodb 过滤日期?