Skip to main content

A small package for CRUD operations for Mongo DB

Project description

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]: 

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_db_operation-1.0.17.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongo_db_operation-1.0.17-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file mongo_db_operation-1.0.17.tar.gz.

File metadata

  • Download URL: mongo_db_operation-1.0.17.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.9

File hashes

Hashes for mongo_db_operation-1.0.17.tar.gz
Algorithm Hash digest
SHA256 cd4236f75722aac50df5eb62775fe05c900ac5794488f3b3fe4e323e5970c90a
MD5 c5fbb77a5bf88d72046cc215cf1f861e
BLAKE2b-256 c4e3598050fa6b317f0b4e249c89a34e72256f35f0c859ca9214adcb4023c69b

See more details on using hashes here.

File details

Details for the file mongo_db_operation-1.0.17-py3-none-any.whl.

File metadata

File hashes

Hashes for mongo_db_operation-1.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 9f896a65eb469a9fec9615503e496257e3ba7df4181961ff7a0bace52f1c6372
MD5 a9c8dce84b22904f7cf0617b53d0f96b
BLAKE2b-256 5c88e20f3f4e158cd4bff27dc233e974e5a987d2883bc64919668a5d9c39dc89

See more details on using hashes here.

Supported by

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