LoginSignup
1
3

More than 5 years have passed since last update.

MongoDBにpythonのdatetimeオブジェクトを保存した時の挙動

Posted at

気になったので実験メモ。

from pymongo import MongoClient
from datetime import datetime
client = MongoClient()
col = client.db.collection
item = col.insert_one({ 'date': datetime.now() })

adminMongoで見たらこんな感じに。
dateキーに挿入されている日付はISODateオブジェクトです。

スクリーンショット 2016-05-31 15.08.37.png

ちなみにpythonでこのデータを読み込むと

items = list(col.find())
items
#[{'_id': ObjectId('574d28258cc7157fb9c98b40'),
#  'date': datetime.datetime(2016, 5, 31, 14, 59, 1, 572000)},
# {'_id': ObjectId('574d28e88cc7157fb9c98b41'),
#  'date': datetime.datetime(2016, 5, 31, 15, 2, 16, 862000)}]

という感じにdatetimeオブジェクトで返ってきます。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3