-
Notifications
You must be signed in to change notification settings - Fork 14.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AIRFLOW-3049] Add extra operations for Mongo hook #3890
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
from ssl import CERT_NONE | ||
|
||
from airflow.hooks.base_hook import BaseHook | ||
from pymongo import MongoClient | ||
from pymongo import MongoClient, ReplaceOne | ||
|
||
|
||
class MongoHook(BaseHook): | ||
|
@@ -130,3 +130,147 @@ def insert_many(self, mongo_collection, docs, mongo_db=None, **kwargs): | |
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
return collection.insert_many(docs, **kwargs) | ||
|
||
def update_one(self, mongo_collection, filter_doc, update_doc, | ||
mongo_db=None, **kwargs): | ||
""" | ||
Updates a single document in a mongo collection. | ||
https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_one | ||
|
||
:param mongo_collection: The name of the collection to update. | ||
:type mongo_collection: str | ||
:param filter_doc: A query that matches the documents to update. | ||
:type filter_doc: dict | ||
:param update_doc: The modifications to apply. | ||
:type update_doc: dict | ||
:param mongo_db: The name of the database to use. | ||
Can be omitted; then the database from the connection string is used. | ||
:type mongo_db: str | ||
|
||
""" | ||
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
return collection.update_one(filter_doc, update_doc, **kwargs) | ||
|
||
def update_many(self, mongo_collection, filter_doc, update_doc, | ||
mongo_db=None, **kwargs): | ||
""" | ||
Updates one or more documents in a mongo collection. | ||
https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_many | ||
|
||
:param mongo_collection: The name of the collection to update. | ||
:type mongo_collection: str | ||
:param filter_doc: A query that matches the documents to update. | ||
:type filter_doc: dict | ||
:param update_doc: The modifications to apply. | ||
:type update_doc: dict | ||
:param mongo_db: The name of the database to use. | ||
Can be omitted; then the database from the connection string is used. | ||
:type mongo_db: str | ||
|
||
""" | ||
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
return collection.update_many(filter_doc, update_doc, **kwargs) | ||
|
||
def replace_one(self, mongo_collection, doc, filter_doc=None, | ||
mongo_db=None, **kwargs): | ||
""" | ||
Replaces a single document in a mongo collection. | ||
https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.replace_one | ||
|
||
If no filter document is given, it is assumed that the replacement | ||
document contains the _id field which is then used as filter. | ||
|
||
:param mongo_collection: The name of the collection to update. | ||
:type mongo_collection: str | ||
:param doc: The new document. | ||
:type doc: dict | ||
:param filter_doc: A query that matches the documents to replace. | ||
Can be omitted; then the _id field from doc will be used. | ||
:type filter_doc: dict | ||
:param mongo_db: The name of the database to use. | ||
Can be omitted; then the database from the connection string is used. | ||
:type mongo_db: str | ||
""" | ||
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
if not filter_doc: | ||
filter_doc = {'_id': doc['_id']} | ||
|
||
return collection.replace_one(filter_doc, doc, **kwargs) | ||
|
||
def replace_many(self, mongo_collection, docs, | ||
filter_docs=None, mongo_db=None, upsert=False, collation=None, | ||
**bulk_kwargs): | ||
""" | ||
Replaces many documents in a mongo collection. | ||
|
||
Uses bulk_write with multiple ReplaceOne operations | ||
https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.bulk_write | ||
|
||
If no filter documents 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. | ||
:type mongo_collection: str | ||
:param docs: The new documents. | ||
:type docs: list(dict) | ||
:param filter_docs: A list of queries that match the documents to replace. | ||
Can be omitted; then the _id fields from docs will be used. | ||
:type filter_docs: list(dict) | ||
:param mongo_db: The name of the database to use. | ||
Can be omitted; then the database from the connection string is used. | ||
:type mongo_db: str | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
""" | ||
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
if not filter_docs: | ||
filter_docs = [{'_id': doc['_id']} for doc in docs] | ||
|
||
requests = [ | ||
ReplaceOne( | ||
filter_docs[i], | ||
docs[i], | ||
upsert=upsert, | ||
collation=collation) | ||
for i in range(len(docs)) | ||
] | ||
|
||
return collection.bulk_write(requests, **bulk_kwargs) | ||
|
||
def delete_one(self, mongo_collection, filter_doc, mongo_db=None, **kwargs): | ||
""" | ||
Deletes a single document in a mongo collection. | ||
https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.delete_one | ||
|
||
:param mongo_collection: The name of the collection to delete from. | ||
:type mongo_collection: str | ||
:param filter_doc: A query that matches the document to delete. | ||
:type filter_doc: dict | ||
:param mongo_db: The name of the database to use. | ||
Can be omitted; then the database from the connection string is used. | ||
:type mongo_db: str | ||
|
||
""" | ||
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
return collection.delete_one(filter_doc, **kwargs) | ||
|
||
def delete_many(self, mongo_collection, filter_doc, mongo_db=None, **kwargs): | ||
""" | ||
Deletes one or more documents in a mongo collection. | ||
https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.delete_many | ||
|
||
:param mongo_collection: The name of the collection to delete from. | ||
:type mongo_collection: str | ||
:param filter_doc: A query that matches the documents to delete. | ||
:type filter_doc: dict | ||
:param mongo_db: The name of the database to use. | ||
Can be omitted; then the database from the connection string is used. | ||
:type mongo_db: str | ||
|
||
""" | ||
collection = self.get_collection(mongo_collection, mongo_db=mongo_db) | ||
|
||
return collection.delete_many(filter_doc, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call this one just
kwargs
as well, to make it congruent with the other calls.