pymongo_crud_use
MongoDB Function Usage Guide
This guide explains the usage of each function provided in the MongoDB function module.
initialize_collection(uri, db_name, collection_name)
1. Initialize and return the MongoDB collection.
Parameters:
-
uri
: MongoDB connection URI. -
db_name
: Name of the MongoDB database. -
collection_name
: Name of the MongoDB collection.
Returns:
- MongoDB collection object.
initialize_collection_with_certificate(uri, certificate_path, db_name, collection_name)
2. Initialize and return the MongoDB collection using TLS certificate authentication.
Parameters:
-
uri
: MongoDB connection URI. -
certificate_path
: Path to the TLS certificate file. -
db_name
: Name of the MongoDB database. -
collection_name
: Name of the MongoDB collection.
Returns:
- MongoDB collection object.
set_data(collection, data)
3. Insert data into the MongoDB collection.
Parameters:
-
collection
: MongoDB collection object. -
data
: Dictionary containing the data to be inserted.
Returns:
- MongoDB insert result.
get_data(collection)
4. Retrieve data from the MongoDB collection where 'available' is True.
Parameters:
-
collection
: MongoDB collection object.
Returns:
- Cursor pointing to the retrieved data.
get_all_data(collection)
5. Retrieve all data from the MongoDB collection.
Parameters:
-
collection
: MongoDB collection object.
Returns:
- Cursor pointing to all data in the collection.
get_data_one(collection, id_data)
6. Retrieve a single document from the MongoDB collection by its ID.
Parameters:
-
collection
: MongoDB collection object. -
id_data
: ID of the document to retrieve.
Returns:
- Single document matching the provided ID.
remove_data_bool(collection, id_data)
7. Remove data from the MongoDB collection by its ID and set 'available' to False.
Parameters:
-
collection
: MongoDB collection object. -
id_data
: ID of the document to remove.
Returns:
- MongoDB update result.
remove_data(collection, id_data)
8. Remove data from the MongoDB collection by its ID.
Parameters:
-
collection
: MongoDB collection object. -
id_data
: ID of the document to remove.
Returns:
- MongoDB delete result.
update_data(collection, id_data, data)
9. Update data in the MongoDB collection by its ID.
Parameters:
-
collection
: MongoDB collection object. -
id_data
: ID of the document to update. -
data
: Updated data as a dictionary.
Returns:
- MongoDB update result.
delete_db(collection)
10. Delete all data from the MongoDB collection.
Parameters:
-
collection
: MongoDB collection object.
Returns:
- MongoDB delete result.
count_db_bool(collection)
11. Count documents in the MongoDB collection where 'available' is True.
Parameters:
-
collection
: MongoDB collection object.
Returns:
- Number of documents where 'available' is True.
count_all_db(collection)
12. Count all documents in the MongoDB collection.
Parameters:
-
collection
: MongoDB collection object.
Returns:
- Total number of documents in the collection.
search_data_by_field(collection, field_name, field_value)
13. Retrieve data from the MongoDB collection based on a specific field and its value.
Parameters:
-
collection
: MongoDB collection object. -
field_name
: Name of the field to search. -
field_value
: Value to search for in the specified field.
Returns:
- Cursor pointing to the retrieved data.
upsert_data(collection, query, data)
14. Update existing data if found, or insert new data if not found.
Parameters:
-
collection
: MongoDB collection object. -
query
: Query to identify the document to update. -
data
: Data to be updated or inserted.
Returns:
- MongoDB update result.
search_across_fields(collection, search_query)
15. Search across all fields of the MongoDB collection and return matching documents.
Parameters:
-
collection
: MongoDB collection object. -
search_query
: Search query string.
Returns:
- Cursor pointing to the retrieved data.
search_all_fields(collection, search_term)
16. Search for the given term in all fields of the MongoDB collection.
Parameters:
-
collection
: MongoDB collection object. -
search_term
: Term to search for in all fields.
Returns:
- Cursor pointing to the retrieved data.
close_connection(collection)
17. Close the connection to the MongoDB database.
Parameters:
-
collection
: MongoDB collection object.
uuid_id()
18. Generate a UUID using the uuid7 · PyPI strategy.
Returns:
- UUID string.