Python MongoDB CRUD
Project description
Python Package for MongoDB CRUD
For use:
- Install package with PIP;
- Import from mongocrud Mongocrud;
- Configure database settings (location, collection, port...);
- Call object CRUD operations;
Example:
- For INSERT operation in database:
```python
from mongocrud import MongoCRUD
from datetime import datetime
dbclients = MongoCRUD("mongodb://localhost","mongoteste1")
dbclients.insert("clients", {"_id": "12345", "name":"Anderson 1", "dtupdate": datetime.now()})
dbclients.insert("clients", {"_id": 15 , "name":"Anderson 2", "dtupdate": datetime.now()})
dbclients.insert("clients", {"name":"Anderson 3", "dtupdate": datetime.now()})
dbclients.insert("clients", {"name":"Anderson 4", "dtupdate": datetime.now()})
dbclients.insert("clients", {"name":"Anderson 5", "dtupdate": datetime.now()})
#12 bits/24 bits info as _id
dbclients.insert("clients", {"_id": ObjectId(b'000000000001'), "name":"Anderson 6", "dtupdate": datetime.now()})
```
- For SELECT operation on database (using orderby and direction for sort information):
```python
from mongocrud import MongoCRUD
from datetime import datetime
dbclients = MongoCRUD("mongodb://localhost","mongoteste1")
clients_ordered = dbclients.select("clients", orderby="dtupdate", direction=1)
for client in clients_ordered: print(client)
```
- For SELECT BY _id (select if using ObjectId on _id):
```python
from mongocrud import MongoCRUD
from datetime import datetime
dbclients = MongoCRUD("mongodb://localhost","mongoteste1")
clients = dbclients.select_by_id("clients", "12345", is_objectid=False)
print(clients)
clients = dbclients.select_by_id("clients", 15, is_objectid=False)
print(clients)
clients = dbclients.select_by_id("clients", "64495d9c140992e498f5fcb2", is_objectid=True)
print(clients)
```
- For DELETE query in database:
```python
from mongocrud import MongoCRUD
from datetime import datetime
dbclients = MongoCRUD("mongodb://localhost","mongoteste1")
items = dbclients.delete("clients", {"name": "Anderson 2"})
print("QTD items deleted => ", items)
```
- For DELETE BY _id:
```python
from mongocrud import MongoCRUD
from datetime import datetime
dbclients = MongoCRUD("mongodb://localhost","mongoteste1")
items = dbclients.delete_by_id("clients", "12345", is_objectid=False)
print("QTD items deleted => ", items)
items = dbclients.delete_by_id("clients", "64495d9c140992e498f5fcb1", is_objectid=True)
print("QTD items deleted => ", items)
items = dbclients.delete_by_id("clients", ObjectId(b"000000000001"), is_objectid=False)
print("QTD items deleted => ", items)
```
- for UPDATE row:
```python
from mongocrud import MongoCRUD
from datetime import datetime
dbclients = MongoCRUD("mongodb://localhost","mongoteste1")
dbclients.update_one("clients", "12345", {"name": "teste1", "dtupdate": datetime.now()}, is_objectid=False)
dbclients.update_one("clients", "64496373fe1ef021a556b446", {"name": "teste2", "dtupdate": datetime.now()}, is_objectid=True)
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mongo_malbizer-1.0.0.tar.gz
(3.1 kB
view hashes)
Built Distribution
Close
Hashes for mongo_malbizer-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e061c1c6f24309ef883ba805d40fbbbe2aabd3cbbc6897ff3848fad102f49525 |
|
MD5 | 386a51d13285093a8c470fdec7298c55 |
|
BLAKE2b-256 | 6c58c8b2ee3f0956185dd667b618043a5f9b9f1153a7c9240fab6f9a4d837ad2 |