blob: 6a759a739c47a0607acf5c7153b38666b0dce987 [file] [log] [blame]
:py:mod:`airflow.providers.mongo.hooks.mongo`
=============================================
.. py:module:: airflow.providers.mongo.hooks.mongo
.. autoapi-nested-parse::
Hook for Mongo DB
Module Contents
---------------
Classes
~~~~~~~
.. autoapisummary::
airflow.providers.mongo.hooks.mongo.MongoHook
.. py:class:: MongoHook(conn_id = default_conn_name, *args, **kwargs)
Bases: :py:obj:`airflow.hooks.base.BaseHook`
Interact with Mongo. This hook uses the Mongo conn_id.
PyMongo Wrapper to Interact With Mongo Database
Mongo Connection Documentation
https://docs.mongodb.com/manual/reference/connection-string/index.html
You can specify connection string options in extra field of your connection
https://docs.mongodb.com/manual/reference/connection-string/index.html#connection-string-options
If you want use DNS seedlist, set `srv` to True.
ex.
{"srv": true, "replicaSet": "test", "ssl": true, "connectTimeoutMS": 30000}
:param mongo_conn_id: The :ref:`Mongo connection id <howto/connection:mongo>` to use
when connecting to MongoDB.
.. py:attribute:: conn_name_attr
:annotation: = conn_id
.. py:attribute:: default_conn_name
:annotation: = mongo_default
.. py:attribute:: conn_type
:annotation: = mongo
.. py:attribute:: hook_name
:annotation: = MongoDB
.. py:method:: __enter__(self)
.. py:method:: __exit__(self, exc_type, exc_val, exc_tb)
.. py:method:: get_conn(self)
Fetches PyMongo Client
.. py:method:: close_conn(self)
Closes connection
.. py:method:: get_collection(self, mongo_collection, mongo_db = None)
Fetches a mongo collection object for querying.
Uses connection schema as DB unless specified.
.. py:method:: aggregate(self, mongo_collection, aggregate_query, mongo_db = None, **kwargs)
Runs an aggregation pipeline and returns the results
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.aggregate
https://pymongo.readthedocs.io/en/stable/examples/aggregation.html
.. py:method:: find(self, mongo_collection, query, find_one = False, mongo_db = None, projection = None, **kwargs)
Runs a mongo find query and returns the results
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.find
.. py:method:: insert_one(self, mongo_collection, doc, mongo_db = None, **kwargs)
Inserts a single document into a mongo collection
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.insert_one
.. py:method:: insert_many(self, mongo_collection, docs, mongo_db = None, **kwargs)
Inserts many docs into a mongo collection.
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.insert_many
.. py:method:: update_one(self, mongo_collection, filter_doc, update_doc, mongo_db = None, **kwargs)
Updates a single document in a mongo collection.
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.update_one
:param mongo_collection: The name of the collection to update.
:param filter_doc: A query that matches the documents to update.
:param update_doc: The modifications to apply.
:param mongo_db: The name of the database to use.
Can be omitted; then the database from the connection string is used.
.. py:method:: update_many(self, mongo_collection, filter_doc, update_doc, mongo_db = None, **kwargs)
Updates one or more documents in a mongo collection.
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.update_many
:param mongo_collection: The name of the collection to update.
:param filter_doc: A query that matches the documents to update.
:param update_doc: The modifications to apply.
:param mongo_db: The name of the database to use.
Can be omitted; then the database from the connection string is used.
.. py:method:: replace_one(self, mongo_collection, doc, filter_doc = None, mongo_db = None, **kwargs)
Replaces a single document in a mongo collection.
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.replace_one
.. note::
If no ``filter_doc`` is given, it is assumed that the replacement
document contain the ``_id`` field which is then used as filters.
:param mongo_collection: The name of the collection to update.
:param doc: The new document.
:param filter_doc: A query that matches the documents to replace.
Can be omitted; then the _id field from doc will be used.
:param mongo_db: The name of the database to use.
Can be omitted; then the database from the connection string is used.
.. py:method:: replace_many(self, mongo_collection, docs, filter_docs = None, mongo_db = None, upsert = False, collation = None, **kwargs)
Replaces many documents in a mongo collection.
Uses bulk_write with multiple ReplaceOne operations
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.bulk_write
.. note::
If no ``filter_docs``are given, it is assumed that all
replacement documents contain the ``_id`` field which are then
used as filters.
:param mongo_collection: The name of the collection to update.
:param docs: The new documents.
:param filter_docs: A list of queries that match the documents to replace.
Can be omitted; then the _id fields from airflow.docs will be used.
:param mongo_db: The name of the database to use.
Can be omitted; then the database from the connection string is used.
:param upsert: If ``True``, perform an insert if no documents
match the filters for the replace operation.
:param collation: An instance of
:class:`~pymongo.collation.Collation`. This option is only
supported on MongoDB 3.4 and above.
.. py:method:: delete_one(self, mongo_collection, filter_doc, mongo_db = None, **kwargs)
Deletes a single document in a mongo collection.
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.delete_one
:param mongo_collection: The name of the collection to delete from.
:param filter_doc: A query that matches the document to delete.
:param mongo_db: The name of the database to use.
Can be omitted; then the database from the connection string is used.
.. py:method:: delete_many(self, mongo_collection, filter_doc, mongo_db = None, **kwargs)
Deletes one or more documents in a mongo collection.
https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.delete_many
:param mongo_collection: The name of the collection to delete from.
:param filter_doc: A query that matches the documents to delete.
:param mongo_db: The name of the database to use.
Can be omitted; then the database from the connection string is used.