Python Mongodb
  • Mongodb কি ?
  • এনভার্নমেন্ট তৈরী :
  • mongodbCompass কি :
    • mongodb compass কানেক্ট এরর :
  • pymongo :
  • mongoengine
    • 01.Connecting With Database
    • 02 Create Document.ipynb
    • 03 Fields
    • 04 FieldValidation
    • 05.Document Relationship One To One
    • 06 Document Relationship One To Many
    • 07 Document Relationship Many To Many
  • MongoDB Atlas
    • Setup
Powered by GitBook
On this page
  • প্রথমে pymongo লাইব্রেরি ইম্পোর্ট করি
  • ডাটাবেজ একসেস করি:
  • Mongodb কোয়েরি :

pymongo :

pymongo পাইথন প্যাকেজ ইনস্টল থাকতে হবে।

pip install pymongo

আমার কাজের সুবিধার জন্য আমি এখানে জুপিটার নোটবুক ব্যবহার করছি অন্য এডিটরেও কাজ করা যাবে।

প্রথমে pymongo লাইব্রেরি ইম্পোর্ট করি

import pymongo

MongoClint সেট করে মঙ্গো সার্ভার এর সাথে কানেক্ট করি

myclint = pymongo.MongoClient('mongodb://localhost:27017/')

ডাটাবেজ একসেস করি:

যদি আগে থেকে ডাটাবেজ না থাকে তাহলে ডাটাবেজ তৈরী হবে। তবে এক্ষেত্রে শর্ত হলো যতক্ষণ পর্যন্ত কোন ডেটা ডেটাবেজ এ ইন্সার্ট করবো। ডেটা ছাড়া মঙ্গো ডাটাবেজ তৈরী করবে না

mydb = myclint['test']

ডেটাবেজ দেখা :

আগে থেকে যে ডেটাবেজ তৈরী করা আছে তা দেখতে নিচের কোড রান করি।

print(myclint.list_database_names())
Output:
['admin', 'config', 'local', 'school', 'test']

কালেকশন বা টেবিল তৈরী /এক্সেস করি

ডেটাবেজ এ একটি কালেকশন বা টেবিল তৈরী /এক্সেস করি :

তবে এক্ষেত্রে শর্ত হলো যতক্ষণ পর্যন্ত কোন ডেটা ডেটাবেজ এ ইন্সার্ট করবো। ডেটা ছাড়া মঙ্গো কালেকশন বা টেবিল তৈরী করবে না

mycol = mydb['customers']

ডেটা ইন্সার্ট করা

mydict = {"name":"gsm ashik","roll":17}
x = mycol.insert_one(mydict)
print(x.inserted_id)
#output 6464e214b13a301bf8a8ce7c (last inserted id generated by mongodb)

একাধিক রও ইন্সার্ট করা

mylist = [
    {
        "name":"Mim","roll":6
    },
    {
       "name":"Mahmuda","roll":7
    }
]
y= mycol.insert_many(mylist)

Mongodb কোয়েরি :

সিঙ্গেল একটি সারি রিটার্ন করা

x = mycol.find_one()
 
print(x)

সব গুলো ডেটা দেখা

for x in mycol.find():
    print(x)
    
#output
'''{'_id': ObjectId('646445eb5baf1ceb41201f0a'), 'name': 'olee ahmmed ashik', 'roll': 25}
{'_id': ObjectId('646451bb5baf1ceb41201f0d'), 'name': 'trek', 'roll': 25}
{'_id': ObjectId('646452155baf1ceb41201f0f'), 'name': 'fokrul', 'roll': 14}
{'_id': ObjectId('6464e214b13a301bf8a8ce7c'), 'name': 'gsm ashik', 'roll': 17}
{'_id': ObjectId('6464e49fb13a301bf8a8ce7d'), 'name': 'Mim', 'roll': 6}
{'_id': ObjectId('6464e49fb13a301bf8a8ce7e'), 'name': 'Mahmuda', 'roll': 7}'''

নির্দিষ্ট কলাম প্রিন্ট করা

cursor = mycol.find()
for result in cursor:
    print(result['name'])

LIMIT

x = mycol.find().limit(5)
for result in x:
    print(result)

sort

cursor = mycol.find().sort([('roll',1)])
for result in cursor:
    print(f"Name: {result['name']}, Roll: {result['roll']}")

ফিল্টার করা :

ফিল্টার করে কোয়েরি করার জন্য কয়েকটি কীওয়ার্ড ব্যবহার করা হয়।

Operation
Syntax
Description

Equality

{“key” : “value”}

Matches values that are equal to a specified value.

Less Than

{“key” :{$lt:”value”}}

Matches values that are less than a specified value.

Greater Than

{“key” :{$gt:”value”}}

Matches values that are greater than a specified value.

Less Than Equal to

{“key” :{$lte:”value”}}

Matches values that are less than or equal to a specified value.

Greater Than Equal to

{“key” :{$gte:”value”}}

Matches values that are greater than or equal to a specified value.

Not Equal to

{“key”:{$ne: “value”}}

Matches all values that are not equal to a specified value.

Logical AND

{ “$and”:[{exp1}, {exp2}, …, {expN}] }

Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.

Logical OR

{ “$or”:[{exp1}, {<exp2}, …, {expN}] }

Joins query clauses with a logical OR returns all documents that match the conditions of either clause.

Logical NOT

{ “$not”:[{exp1}, {exp2}, …, {expN}] }

Inverts the effect of a query expression and returns documents that do not match the query expression.

Equality :

cursor = mycol.find({"name":"trek"})
for record in cursor:
    print(record)

Not Equal To

# Not Equal To
cursor = mycol.find({'name':{"$ne":"Mim"}})
for record in cursor:
    print(record)

Less Than

# Less Than
cursor = mycol.find({"roll":{'$lt':25}})
for record in cursor:
    print(record)

Greater Than

# Greater Than
cursor = mycol.find({"roll":{"$gt":20}})
for record in cursor:
    print(record)

IN

cursor = mycol.find({'name': {'$in': ['Mim', 'Mahmuda']}})
for record in cursor:
    print(record)

AND

cursor = mycol.find({'$and': [{'name': 'Mim'}, {'roll': 6}]})
for record in cursor:
    print(record)

OR

cursor = mycol.find({'$or': [{'name': 'Mim'}, {'roll': 7}]})
for record in cursor:
    print(record)

আপডেট রও :

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['test']
mycol = mydb['customers']

# Define the filter criteria to identify the document to update
filter = {'name': 'olee', 'roll': 7}

# Define the new values to update in the document
new_values = {'$set': {'age': 25, 'city': 'New York'}}

# Update the document
mycol.update_one(filter, new_values)

print("Document updated successfully.")

ডিলেট রো :

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['test']
mycol = mydb['customers']


# Define the filter criteria
filter = {'name': 'olee ahmmed ashik', 'roll': 25}

# Delete the document
mycol.delete_one(filter)

print("Document deleted successfully.")

ডিলেট ডকুমেন্ট :

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['test']
mycol = mydb['customers']

# Delete all documents in the collection
mycol.delete_many({})

print("All documents deleted successfully.")
Previousmongodb compass কানেক্ট এরর :Nextmongoengine

Last updated 1 year ago