nailo2c opened a new pull request, #50518:
URL: https://github.com/apache/airflow/pull/50518

   related: #33076
   
   This PR adds a `create_collection()` function to the `MongoHook` which 
refers to 
[pymongo.database.Database.create_collection](https://pymongo.readthedocs.io/en/stable/api/pymongo/database.html#pymongo.database.Database.create_collection).
   
   Before:
   ```python
   mongo_hook = MongoHook(conn_id="mongo_default")
   mongo_conn = mongo_hook.get_conn()
   mongo_db = mongo_conn["db_name"]
   
   # create time collection here
   mongo_db.create_collection("time_test", timeseries= {
       "timeField": "timestamp",
       "metaField": "metadata",
       "granularity": "hours"
   })
   
   # use the collection
   mongo_collection = mongo_db["time_test"]
   
   # add document
   mongo_collection.insert_one(
     {
       "metadata": { "sensorId": 5578, "type": "temperature" },
       "timestamp": dt.utcnow(),
       "temp": 12
     }
   )
   ```
   
   After
   ```python
   mongo_hook = MongoHook(conn_id="mongo_default")
   
   # create (or get) the time-series collection in one call
   mongo_collection = mongo_hook.create_collection(
       name="time_test",
       timeseries={
           "timeField": "timestamp",
           "metaField": "metadata",
           "granularity": "hours",
       },
   )
   
   # add document
   mongo_collection.insert_one(
       {
           "metadata": {"sensorId": 5578, "type": "temperature"},
           "timestamp": dt.utcnow(),
           "temp": 12,
       }
   )
   ```
   
   I’ve completed an end-to-end test on my local Airflow instance, and 
everything worked as expected.
   ![截圖 2025-05-12 下午7 28 
06](https://github.com/user-attachments/assets/c28dea2f-8e9a-4c3e-8217-354fb47cb980)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to