Behavior when saving python datetime object in MongoDB

I was curious, so I made an experiment memo.

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

Looking at adminMongo, it looks like this. The date inserted in the date key is an ISODate object.

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

By the way, if you read this data with 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)}]

It will be returned as a datetime object.

Recommended Posts

Behavior when saving python datetime object in MongoDB
Behavior when listing in Python heapq
Object oriented in python
Create a datetime object from a string in Python (Python 3.3)
String object methods in Python
Null object comparison in Python
Use MongoDB ODM in Python
python in mongodb in descending sort
Attention when os.mkdir in Python
Precautions when using pit in Python
Handling timezones in Python (datetime, pytz)
When using regular expressions in Python
When writing a program in Python
Timezone specification when converting a string to datetime type in python
When specifying multiple keys in python sort
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
Behavior when returning in the with block
Create ScriptableObject in Python when building ADX2
Precautions when pickling a function in python
Create a JSON object mapper in Python
[python, multiprocessing] Behavior for exceptions when using multiprocessing
When looking at memory usage in Python 3
MongoDB for the first time in Python
[Python] Saving learning results (models) in machine learning
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
[Tips] Easy-to-read writing when connecting functions in Python
When codec can't decode byte appears in python
Pickle.dumped object dies when pickle.loaded in another object hierarchy
[python, CPython] GC behavior when throwing an exception
Precautions when dealing with control structures in Python 2.6
Note on encoding when LANG = C in Python
Character encoding when dealing with files in Python 3
I tried non-blocking I / O Eventlet behavior in Python
Split files when writing vim plugin in python
About the behavior of Model.get_or_create () of peewee in Python
Difference in object ID due to import in Python
[Question] What happens when I use% in python?
Error when trying to install psycopg2 in Python
Solution when module'XXX' has no attribute'XXX' in Python
How to handle datetime type in python sqlite3
Insert an object inside a string in Python
[Web development with Python] Precautions when saving cookies
Use pydantic when reading environment variables in Python
Settings for getting started with MongoDB in python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Python built-in object
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
Python built-in object