0

I am trying to insert some data with (ISO-formatted) dates in MongoDB.

My json file looks like this:

[
  { "name": "Alice", "born": { "$date": "2018-11-10T22:26:12.111Z" }}, 
  { "name": "Bob",  "born": { "$date": "2019-12-04T21:10:14.111Z" }}
]

When I insert this file using mongoimport, it successfully inserts the data and dates are indeed recognized as ISODate objects.

mongoimport -d mydb -c myollection --jsonArray --type json data.json
mydb> db["mycollection"].find({}).limit(1)
[
  {
    _id: ObjectId('66717ca2a13e22510be15f35'),
    name: 'Alice',
    born: ISODate('2018-11-10T22:26:12.111Z')
  }
]

However, when I insert the same JSON file with a small Python snippet, dates are inserted as dictionaries and not recognized as ISODate objects.

from pymongo.mongo_client import MongoClient
import json

if __name__ == '__main__':
    with MongoClient() as mongo:
        db = mongo.get_database("mydb")
        with open("data.json", "r") as data_file:
            my_tuples = json.load(data_file)
            db["mycollectionII"].insert_many(my_tuples, ordered=False)

shows:

mydb> db["mycollectionII"].find({}).limit(1)
[
  {
    _id: ObjectId('66717d6d59b2222b5b7cd9e4'),
    name: 'Alice',
    born: { '$date': '2018-11-10T22:26:12.111Z' }
  }
]

My input data is generated by another system so I cannot afford to create in-memory dictionaries (with Python) and create a datetime object for each date.

My question is: how to insert a JSON file containing ISODate-like objects in MongoDB using pymongo (such that these dates are "understood" as dates and not simple strings)? What I need in the end is to be able to query those dates with $gt, $lt, and range queries (which I cannot do if they are simple strings).

3
  • I guess you have to convert the "$date": "2019-12-04T21:10:14.111Z" string into a datetime object Commented Jun 18, 2024 at 13:07
  • 3
    Use the loads function for bson instead of json - bson.json_util.loads as shown in all the linked duplicates. And then replace the tuples line with my_tuples = bson.json_util.loads(data_file.read()) Commented Jun 18, 2024 at 13:13
  • I will test the BSON approach right now and let you know! Commented Jun 18, 2024 at 13:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.