Skip to main content

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:
from mongocrud import MongoCRUD, ObjectId
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):
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):
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:
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:
from mongocrud import MongoCRUD, ObjectId
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:
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


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.1.tar.gz (3.1 kB view hashes)

Uploaded Source

Built Distribution

mongo_malbizer-1.0.1-py3-none-any.whl (3.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page