Package: electrodb
ElectroDB DatabaseThis project provides a reliable database, allowing you to perform CRUD operations and more on collections of documents.FeaturesInsert single and multiple documentsUpdate documents using various operatorsDelete documents based on filtersFind documents based on filtersFetch all documents or with a specified queryCount documents that match a queryRetrieve distinct values of a fieldDrop indexesFind and update a single documentFind and replace a single documentFind and delete a single documentPerform bulk write operationsList collections, indexes, and collection namesReplace documents based on filtersRename a collectionEstimate document countSort documents based on a fieldAggregate documents using a pipelineCreate an index on a fieldDrop a collectionFind or insert a document using upsertRetrieve distinct values of a field with a queryPerform map-reduce operationsFind documents by emailValidate data based on a queryCheck if a collection is cappedCompact the database by removing deleted documentsUsagefromelectrodbimportElectronclient=Electron(host='localhost',port=37017,username='root',password='root')db=client['my_database']collection=db['users']# Insert one documentinsert_result=collection.insert_one({"name":"Alice","age":30})print(insert_result)# Insert multiple documentsinsert_many_result=collection.insert_many([{"name":"Bob","age":25},{"name":"Charlie","age":28}])print(insert_many_result)# Update a documentupdate_result=collection.update_one({"name":"Alice"},{"$set":{"age":31}})print(update_result)# Update multiple documentsupdate_many_result=collection.update_many({"age":{"$lt":30}},{"$set":{"status":"young"}})print(update_many_result)# Delete a documentdelete_result=collection.delete_one({"name":"Bob"})print(delete_result)# Delete multiple documentsdelete_many_result=collection.delete_many({"age":{"$lt":30}})print(delete_many_result)# Find documents using a queryquery={"status":"young"}found_documents=collection.fetch_all(query)print(found_documents)# Count documents matching a querycount=collection.count_documents(query)print(count)# Get distinct values for a fielddistinct_values=collection.distinct("age",query)print(distinct_values)# Create an indexindex_result=collection.create_index("age")print(index_result)# List collection namescollection_names=collection.list_collection_names()print(collection_names)# Drop a collectiondrop_result=collection.drop()print(drop_result)# Find one document and upsertupsert_result=collection.find_one_and_upsert({"name":"David"},{"name":"David","age":22})print(upsert_result)# Validate data based on a queryvalidate_query={"age":30}validated_data=collection.validate_data(validate_query)print(validated_data)# Check if collection is cappedis_capped=collection.is_capped()print(is_capped)# Compact the databasecompact_result=collection.compact_database()print(compact_result)# List collections in the databasecollection_list=collection.list_collections()print(collection_list)# List indexes in the collectionindex_list=collection.list_indexes()print(index_list)# Rename the collectionrename_result=collection.rename_collection("new_collection_name")print(rename_result)# Get estimated document countdocument_count=collection.estimated_document_count()print(document_count)# Sort documents by a fieldsorted_documents=collection.sort("age",reverse=True)print(sorted_documents)# Aggregate documents using a pipelinepipeline=[{"$sort":{"field":"age","order":1}},{"$project":["name","age"]}]aggregated_result=collection.aggregate(pipeline)print(aggregated_result)# Find one document and replacereplace_result=collection.find_one_and_replace({"name":"David"},{"name":"Ella","age":27})print(replace_result)# Find one document and deletedelete_result=collection.find_one_and_delete({"name":"Ella"})print(delete_result)# Perform map-reduce operationdefmap_function(item):return{"age":item["age"]}defreduce_function(key,values):returnsum(values)/len(values)map_reduce_result=collection.map_reduce(map_function,reduce_function)print(map_reduce_result)# Find documents by emailemail_search_result=collection.find_by_email("alice@example.com")print(email_search_result)# Distinct values with a querydistinct_query_result=collection.distinct_with_query("age",{"status":"young"})print(distinct_query_result)# Drop an indexdrop_index_result=collection.drop_index("age_1")print(drop_index_result)# Define bulk write operationsbulk_operations=[{"insert_one":{"name":"Alice","email":"alice@example.com","age":25}},{"insert_many":[{"name":"Bob","email":"bob@example.com","age":30},{"name":"Charlie","email":"charlie@example.com","age":28}]},{"update_one":{"filter":{"age":{"$gt":25}},"update":{"$set":{"status":"senior"}}}},{"update_many":{"filter":{"age":{"$lt":30}},"update":{"$set":{"status":"young"}}}},{"delete_one":{"age":{"$gt":25}}},{"delete_many":{"age":{"$lt":30}}}]# Use the bulk_write function to perform the defined operationsresult=collection.bulk_write(bulk_operations)RecoveryfromelectrodbimportRecovery,Electronclient=Electron(host='localhost',port=37017,username='root',password='root')db=client['my_database']collection=db['users']# Create a recovery instancerecovery=Recovery(client)# Backup a databasebackup_result=recovery.backup('my_database',format='zip')print(backup_result)# Backup of 'my_database' created at '/path/to/backup/my_database.zip'.# Restore a databaserestore_result=recovery.restore('/path/to/backup/my_database.zip',format='zip')print(restore_result)# Database restored from 'zip' backup.