Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Gandori committed Aug 22, 2024
1 parent 8f1dd07 commit 3750cab
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ Features
| Feature | Impemented/NotImplemented |
| - | - |
| collection.find_one() | Implemented |
| collection.find_one_with_id() | Implemented |
| collection.find_one_and_delete() | NotImplemented |
| collection.find_one_and_replace() | NotImplemented |
| collection.find_one_and_update() | NotImplemented |
| collection.find() | Implemented |
| collection.find_raw_batches() | NotImplemented |
| collection.insert_one() | Implemented |
| collection.insert_many() | Implemented |
| collection.insert_one() the document param supports pure pydantic model | NotImplemented |
| collection.insert_many() the documents param supports list of pydantic models | NotImplemented |
| collection.update_one() | Implemented |
| collection.update_many() | NotImplemented |
| collection.delete_one() | Implemented |
Expand Down Expand Up @@ -43,4 +45,65 @@ Placeholder

### Simple Example

Placeholder
```python
import asyncio
from typing import Any

from bson import ObjectId
from pydantic import BaseModel

from simple_mongodb import BaseCollection, MongoDBClient


class AccountCollection(BaseCollection):
db = 'my-db' # The name of the database or set the enviroment variable MONGODB_DB
collection = 'account-collection' # The name of the collection


class Account(BaseModel):
name: str


async def main() -> None:
# Initialize a client object and pass the url or set enviroment variables
# MONGODB_HOST, MONGODB_PORT,
# MONGODB_USERNAME, MONGODB_PASSWORD
# Is the url param or enviroment variables not set the default values are used
client: MongoDBClient = MongoDBClient(url='mongodb://user:pass@host:27017')

# Initialize the account collection
account_collection: AccountCollection = AccountCollection(client=client)

account: Account = Account(name='example-name')

try:

# Insert the document in the collection
document: dict[str, Any] = account.model_dump()
inserted_id: ObjectId = await account_collection.insert_one(document=document)

# Find the document
where: dict[str, Any] = {'_id': inserted_id}
document: dict[str, Any] = await account_collection.find_one(where=where)

# Update the document
update: dict[str, Any] = {'$set': {'name': 'other-name'}}
# Returns the id of the new document if upsert=True
await account_collection.update_one(where=where, update=update, upsert=False)

except account_collection.InsertError:
pass
except account_collection.FindError:
pass
except account_collection.UpdateError:
pass
except account_collection.ServerTimeoutError:
pass

# Close the db connection
client.close()


if __name__ == '__main__':
asyncio.run(main())
```

0 comments on commit 3750cab

Please sign in to comment.