使用 mongodb 和 python 设置正确的数据类型

Set correct data type with mongodb and python

我刚刚开始通过 pyhton 驱动器 (pymongo) 使用 mongodb。当我发布新数据(实际上来自 MySQL 数据库)时,某些数据类型似乎映射不正确。例如,一些单个数字作为长整型插入,时间戳作为字符串插入。此外,存储在 MySQL 中的 YY-MM-DD 日期更改为 YY-MM-DD 00:00:00(即添加时间)。这似乎是对 space 的浪费,这是 mongodb 的正常程序还是我应该以某种方式更改映射不正确(?)的数据类型?

ps 我确实以 mongodb 的身份搜索了文档,但找不到任何符合我的查询的内容。

post = {
    "title": video_title,
    "ext_id": video_external_id,
    "source": video_source,
    "date_added": d1,
    "views":{
              "views_all": views_all,
              "views_year": views_yr,
              "views_day": views_day,
              "views_week": views_wk,
              "views_month": views_mo
              },
    "video_type": 0,
    "hd": video_hd,
    "features": featured,
    "video_thumbs": video_thumbnails,
    "video_main_thumb": video_main_thumbnail,
    "length": video_length,
    "length_sort": video_length,

    "rating": {
              "rating_all": rating_all,
              "rating_year": rating_yr,
              "rating_day": rating_day,
              "rating_week": rating_wk,
              "rating_month": rating_mo
              }
        }
posts = db.posts
post_id = video_list.insert(post)

For example, some single digit number are inserted as long ints

PyMongo 将 python 2.x long 存储为 BSON int64 而不管值如何,将 python int 存储为 BSON int32 BSON int64 取决于int 的值。有关 python 类型到 BSON 类型的映射,请参阅 the table here

a timestamp is inserted as a string

假设时间戳以正确的 ISO-8601 格式传递。如果您想将时间戳存储为 BSON 日期时间,请改为传递 python 日期时间对象。

Also the date which is stored in MySQL as YY-MM-DD is changed to YY-MM-DD 00:00:00 (i.e a time is added)

BSON(MongoDB使用的存储格式)没有没有时间元素的日期类型。将日期转换为日期时间取决于您的应用程序。