Package: mangodm
MANGO - simple async ODM for MongoDB.User GuideInstallationYou can installmangodmviapip.pipinstallmangodmInitializationFirstly need to connect to MongoDB.frommangodmimportconnect_to_mongoMONGODB_CONNECTION_URL=""DATABASE_NAME=""asyncdefmain():awaitconnect_to_mongo(MONGODB_CONNECTION_URL,DATABASE_NAME)Don't forgot to close connection.frommangodmimportconnect_to_mongo,close_connectionMONGODB_CONNECTION_URL=""DATABASE_NAME=""defbefor_down():close_connection()asyncdefmain():awaitconnect_to_mongo(MONGODB_CONNECTION_URL,DATABASE_NAME)Defining CollectionInheritDocumentclass and describe its fields to defining new collection.frommangodmimportDocumentclassUser(Document):name:strage:intBefore start to use collection you need to register it.frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()# IMPORTANTAlso you can add special configuration to collection. To do this add subclassConfig.Config Parameterscollection_name - collection name in mongoDB.excludeFields - list of names fields which don't saving in DB.excludeFieldsResponse - list of names fields which don't include in response.frommangodmimportDocumentclassUser(Document):name:strpassword:strage:intpoints:intclassConfig:collection_name="users"excludeFields=["points"]excludeFieldsResponse:["password"]User.register_collection()Create new documentFor create new document just create new object of collection class and callcreate()method from it.frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()asyncdefmain():mike=User(name="Mike",age=23)awaitmike.create()After creating you can get documentid. ID isstrtype.asyncdefmain():mike=User(name="Mike",age=23)awaitmike.create()print(mike.id)Get documents from MongoDBTo get one document use method -get. Parameters for this method is filter for finding document. Parameter convert to JSON and put to MongoDB methodfind_one.Method return class object ornoneif querying document doesn't exist.frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()asyncdefmain():mike=awaitUser.get(name="Mike")ifmike:print(mike.id)anna=awaitUser.get(id="6561a3851492b7e9d9a9533e")ifanna:print(anna.age)To get all documets which suitable for finding filter use method -find. This method has same paramrtrs how methodget.findmethod return list of class objects, list can be empty.frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()asyncdefmain():users=awaitUser.find()# get all documents if no parametrsforuserinusers:print(user.id)Updating documentTo update editing use methodupdate.frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()asyncdefmain():mike=awaitUser.get(name="Mike")ifmike:print(mike.id)mike.age=24awaitmike.update()Deleting documentTo delete document use methoddelete.frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()asyncdefmain():mike=awaitUser.get(name="Mike")ifmike:awaitmike.delete()Response documentsYou can convert your document to JSON. For this use methodto_response().frommangodmimportDocumentclassUser(Document):name:strage:intUser.register_collection()asyncdefmain():mike=awaitUser.get(name="Mike")ifmike:print(mike.to_response())