Skip to main content

MongoDB Python CRUD Operations

This project offers an easy-to-use Python interface that enables you to perform CRUD (Create, Read, Update, Delete) operations on a MongoDB database. It uses the PyMongo library and is designed to work seamlessly with a prebuilt MongoDB instance installed on your system or hosted elsewhere. To use this code, you'll need a prebuilt MongoDB instance that's up and running. In my case, I created a MongoDB instance through https://cloud.mongodb.com/. However, you can also run your database locally or use a different provider if you prefer. Just make sure that your MongoDB instance is configured correctly before you start working with the code. To ensure that the code can connect to your MongoDB instance, it's important to whitelist the appropriate IP addresses. This can be done using a mask or by specifying specific IP addresses. Before you start working with the code, make sure that your MongoDB instance is reachable via whitelisting. This will ensure that your code can access the database without any issues.

image

Clone Repo

git clone https://github.com/melihteke/mongo-db-operation
pip install -r requirements.txt

Package Installation

pip install mongo-db-operation

Pypi URL

https://pypi.org/project/mongo-db-operation/

Create .env file in to relative root directory and fill in below variables

DB_URI=mongodb+srv://<DB_USERNAME>:<DB_PASSWORD>@cluster0.ybl2347fog.mongodb.net/test?retryWrites=true&w=majority
DB_NAME=<DB_NAME>
DB_COLLECTION_NAME=<DB_COLLECTION_NAME>

Usage

from mongo_db_operation.mongo_client import MongoDBOperation
from mongo_db_operation import environment

operation = MongoDBOperation()

In [8]: operation.view_all_records()
                   add_record()           db                     update_record()       
                   client                 environment            view_all_records()    
                   collection             remove_record()        view_specific_record()
Viewing all the records
In [8]: operation.view_all_records()
Out[8]: 
[{'_id': ObjectId('64370dbf0971a02bdf950163'),
  'hostname': 'DEV2010R01',
  'device_type': 'router',
  'site_number': '2010',
  'site': 'Craydon',
  'status': 'Operational',
  'local_subnets': ['10.10.1.0/24', '10.10.2.0/24', '10.10.3.0/24'],
  'wan_a_provider': 'BT',
  'wan_a_ip_setting': 'dhcp',
  'wan_b_provider': 'BT',
  'wan_b_ip_setting': '32.45.31.141/30',
  'hardware': 'C1111-4PLTEEA',
  'serial': 'FOCXXSDF5K9',
  'vlans': ['8', '110'],
  'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
  'l2_ints': ['GigabitEthernet0/1/0',
   'GigabitEthernet0/1/1',
   'GigabitEthernet0/1/2',
   'GigabitEthernet0/1/3']}]
}
Adding Records:
In [9]: data = {  "hostname": "DEV2012R01",
  ...:   "device_type":"router",
  ...:   "site_number":"2012",
  ...:   "site": "Dublin",
  ...:   "status":"Operational",
  ...:   "local_subnets":["10.12.1.0/24", "10.12.2.0/24", "10.12.3.0/24"],
  ...:   "wan_a_provider":"GTT",
  ...:   "wan_a_ip_setting":"dhcp",
  ...:   "wan_b_provider":"BT",
  ...:   "wan_b_ip_setting":"213.45.23.143/30",
  ...:   "hardware": "C1111-4PLTEEA",
  ...:   "serial":"FOC2XXX7L599",
  ...:   "vlans":["8", "110"],
  ...:   "l3_ints":["GigabitEthernet0/0/0", "GigabitEthernet0/0/1"],
  ...:   "l2_ints":["GigabitEthernet0/1/0","GigabitEthernet0/1/1", "GigabitEthernet0/1/2", "GigabitEthernet0/1/3"]
  ...: }

In [10]: operation.add_record(data)

In [11]: operation.view_all_records()
Out[11]: 
[{'_id': ObjectId('64370dbf0971a02bdf950163'),
 'hostname': 'DEV2010R01',
 'device_type': 'router',
 'site_number': '2010',
 'site': 'Craydon',
 'status': 'Operational',
 'local_subnets': ['10.10.1.0/24', '10.10.2.0/24', '10.10.3.0/24'],
 'wan_a_provider': 'BT',
 'wan_a_ip_setting': 'dhcp',
 'wan_b_provider': 'BT',
 'wan_b_ip_setting': '32.45.31.141/30',
 'hardware': 'C1111-4PLTEEA',
 'serial': 'FOCXXSDF5K9',
 'vlans': ['8', '110'],
 'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
 'l2_ints': ['GigabitEthernet0/1/0',
  'GigabitEthernet0/1/1',
  'GigabitEthernet0/1/2',
  'GigabitEthernet0/1/3']},
{'_id': ObjectId('64370e340971a02bdf950164'),
 'hostname': 'DEV2012R01',
 'device_type': 'router',
 'site_number': '2012',
 'site': 'Dublin',
 'status': 'Operational',
 'local_subnets': ['10.12.1.0/24', '10.12.2.0/24', '10.12.3.0/24'],
 'wan_a_provider': 'GTT',
 'wan_a_ip_setting': 'dhcp',
 'wan_b_provider': 'BT',
 'wan_b_ip_setting': '213.45.23.143/30',
 'hardware': 'C1111-4PLTEEA',
 'serial': 'FOC2XXX7L599',
 'vlans': ['8', '110'],
 'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
 'l2_ints': ['GigabitEthernet0/1/0',
  'GigabitEthernet0/1/1',
  'GigabitEthernet0/1/2',
  'GigabitEthernet0/1/3']}]

In [12]: 
Updating a record
In [12]: data_to_be_updated = {  "hostname": "DEV2012R01",
    ...:   "device_type":"router",
    ...:   "site_number":"2012",
    ...:   "site": "Dublin_XXXXXXXXXXXXXXXX",
    ...:   "status":"Operational",
    ...:   "local_subnets":["10.12.1.0/24", "10.12.2.0/24", "10.12.3.0/24"],
    ...:   "wan_a_provider":"GTT",
    ...:   "wan_a_ip_setting":"dhcp",
    ...:   "wan_b_provider":"BT",
    ...:   "wan_b_ip_setting":"213.45.23.143/30",
    ...:   "hardware": "C1111-4PLTEEA",
    ...:   "serial":"FOC2XXX7L599",
    ...:   "vlans":["8", "110"],
    ...:   "l3_ints":["GigabitEthernet0/0/0", "GigabitEthernet0/0/1"],
    ...:   "l2_ints":["GigabitEthernet0/1/0","GigabitEthernet0/1/1", "GigabitEthernet0/1/2", "GigabitEthernet0/1/3"]
    ...: }

In [13]: operation.update_record('64370e340971a02bdf950164',data_to_be_updated)

In [15]: operation.view_all_records()
Out[15]: 
[{'_id': ObjectId('64370dbf0971a02bdf950163'),
  'hostname': 'DEV2010R01',
  'device_type': 'router',
  'site_number': '2010',
  'site': 'Craydon',
  'status': 'Operational',
  'local_subnets': ['10.10.1.0/24', '10.10.2.0/24', '10.10.3.0/24'],
  'wan_a_provider': 'BT',
  'wan_a_ip_setting': 'dhcp',
  'wan_b_provider': 'BT',
  'wan_b_ip_setting': '32.45.31.141/30',
  'hardware': 'C1111-4PLTEEA',
  'serial': 'FOCXXSDF5K9',
  'vlans': ['8', '110'],
  'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
  'l2_ints': ['GigabitEthernet0/1/0',
   'GigabitEthernet0/1/1',
   'GigabitEthernet0/1/2',
   'GigabitEthernet0/1/3']},
 {'_id': ObjectId('64370e340971a02bdf950164'),
  'hostname': 'DEV2012R01',
  'device_type': 'router',
  'site_number': '2012',
  'site': 'Dublin_XXXXXXXXXXXXXXXX',
  'status': 'Operational',
  'local_subnets': ['10.12.1.0/24', '10.12.2.0/24', '10.12.3.0/24'],
  'wan_a_provider': 'GTT',
  'wan_a_ip_setting': 'dhcp',
  'wan_b_provider': 'BT',
  'wan_b_ip_setting': '213.45.23.143/30',
  'hardware': 'C1111-4PLTEEA',
  'serial': 'FOC2XXX7L599',
  'vlans': ['8', '110'],
  'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
  'l2_ints': ['GigabitEthernet0/1/0',
   'GigabitEthernet0/1/1',
   'GigabitEthernet0/1/2',
   'GigabitEthernet0/1/3']}]

In [16]: 
Removing a record
In [16]: operation.view_all_records()
Out[16]: 
[{'_id': ObjectId('64370dbf0971a02bdf950163'),
  'hostname': 'DEV2010R01',
  'device_type': 'router',
  'site_number': '2010',
  'site': 'Craydon',
  'status': 'Operational',
  'local_subnets': ['10.10.1.0/24', '10.10.2.0/24', '10.10.3.0/24'],
  'wan_a_provider': 'BT',
  'wan_a_ip_setting': 'dhcp',
  'wan_b_provider': 'BT',
  'wan_b_ip_setting': '32.45.31.141/30',
  'hardware': 'C1111-4PLTEEA',
  'serial': 'FOCXXSDF5K9',
  'vlans': ['8', '110'],
  'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
  'l2_ints': ['GigabitEthernet0/1/0',
   'GigabitEthernet0/1/1',
   'GigabitEthernet0/1/2',
   'GigabitEthernet0/1/3']},
 {'_id': ObjectId('64370e340971a02bdf950164'),
  'hostname': 'DEV2012R01',
  'device_type': 'router',
  'site_number': '2012',
  'site': 'Dublin_XXXXXXXXXXXXXXXX',
  'status': 'Operational',
  'local_subnets': ['10.12.1.0/24', '10.12.2.0/24', '10.12.3.0/24'],
  'wan_a_provider': 'GTT',
  'wan_a_ip_setting': 'dhcp',
  'wan_b_provider': 'BT',
  'wan_b_ip_setting': '213.45.23.143/30',
  'hardware': 'C1111-4PLTEEA',
  'serial': 'FOC2XXX7L599',
  'vlans': ['8', '110'],
  'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
  'l2_ints': ['GigabitEthernet0/1/0',
   'GigabitEthernet0/1/1',
   'GigabitEthernet0/1/2',
   'GigabitEthernet0/1/3']}]

In [17]: operation.remove_record('64370e340971a02bdf950164')

In [18]: operation.view_all_records()
Out[18]: 
[{'_id': ObjectId('64370dbf0971a02bdf950163'),
  'hostname': 'DEV2010R01',
  'device_type': 'router',
  'site_number': '2010',
  'site': 'Craydon',
  'status': 'Operational',
  'local_subnets': ['10.10.1.0/24', '10.10.2.0/24', '10.10.3.0/24'],
  'wan_a_provider': 'BT',
  'wan_a_ip_setting': 'dhcp',
  'wan_b_provider': 'BT',
  'wan_b_ip_setting': '32.45.31.141/30',
  'hardware': 'C1111-4PLTEEA',
  'serial': 'FOCXXSDF5K9',
  'vlans': ['8', '110'],
  'l3_ints': ['GigabitEthernet0/0/0', 'GigabitEthernet0/0/1'],
  'l2_ints': ['GigabitEthernet0/1/0',
   'GigabitEthernet0/1/1',
   'GigabitEthernet0/1/2',
   'GigabitEthernet0/1/3']}]

In [19]: 

Metadata

Release files for mongo-db-operation 1.0.17

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongo-db-operation 1.0.17
File Size Uploaded Provenance
mongo_db_operation-1.0.17.tar.gz 15.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mongo-db-operation 1.0.17
File Interpreter ABI Platform
mongo_db_operation-1.0.17-py3-none-any.whl Python 3 none any Details

Total release size: 32.1 kB

Release files / mongo_db_operation-1.0.17.tar.gz

Download URL mongo_db_operation-1.0.17.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cd4236f75722aac50df5eb62775fe05c900ac5794488f3b3fe4e323e5970c90a
BLAKE2b-256 checksum
How to use checksums
c4e3598050fa6b317f0b4e249c89a34e72256f35f0c859ca9214adcb4023c69b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.9

Release files / mongo_db_operation-1.0.17-py3-none-any.whl

Download URL mongo_db_operation-1.0.17-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f896a65eb469a9fec9615503e496257e3ba7df4181961ff7a0bace52f1c6372
BLAKE2b-256 checksum
How to use checksums
5c88e20f3f4e158cd4bff27dc233e974e5a987d2883bc64919668a5d9c39dc89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.9

Security for mongo-db-operation 1.0.17

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page