pymongo পাইথন প্যাকেজ ইনস্টল থাকতে হবে।
আমার কাজের সুবিধার জন্য আমি এখানে জুপিটার নোটবুক ব্যবহার করছি অন্য এডিটরেও কাজ করা যাবে।
প্রথমে pymongo লাইব্রেরি ইম্পোর্ট করি
MongoClint সেট করে মঙ্গো সার্ভার এর সাথে কানেক্ট করি
Copy myclint = pymongo.MongoClient('mongodb://localhost:27017/')
ডাটাবেজ একসেস করি:
যদি আগে থেকে ডাটাবেজ না থাকে তাহলে ডাটাবেজ তৈরী হবে। তবে এক্ষেত্রে শর্ত হলো যতক্ষণ পর্যন্ত কোন ডেটা ডেটাবেজ এ ইন্সার্ট করবো। ডেটা ছাড়া মঙ্গো ডাটাবেজ তৈরী করবে না
Copy mydb = myclint['test']
ডেটাবেজ দেখা :
আগে থেকে যে ডেটাবেজ তৈরী করা আছে তা দেখতে নিচের কোড রান করি।
Copy print(myclint.list_database_names())
Output:
['admin', 'config', 'local', 'school', 'test']
কালেকশন বা টেবিল তৈরী /এক্সেস করি
ডেটাবেজ এ একটি কালেকশন বা টেবিল তৈরী /এক্সেস করি :
তবে এক্ষেত্রে শর্ত হলো যতক্ষণ পর্যন্ত কোন ডেটা ডেটাবেজ এ ইন্সার্ট করবো। ডেটা ছাড়া মঙ্গো কালেকশন বা টেবিল তৈরী করবে না
Copy mycol = mydb['customers']
ডেটা ইন্সার্ট করা
Copy mydict = {"name":"gsm ashik","roll":17}
x = mycol.insert_one(mydict)
print(x.inserted_id)
#output 6464e214b13a301bf8a8ce7c (last inserted id generated by mongodb)
একাধিক রও ইন্সার্ট করা
Copy mylist = [
{
"name":"Mim","roll":6
},
{
"name":"Mahmuda","roll":7
}
]
y= mycol.insert_many(mylist)
Mongodb কোয়েরি :
সিঙ্গেল একটি সারি রিটার্ন করা
Copy x = mycol.find_one()
print(x)
সব গুলো ডেটা দেখা
Copy 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}'''
নির্দিষ্ট কলাম প্রিন্ট করা
Copy cursor = mycol.find()
for result in cursor:
print(result['name'])
LIMIT
Copy x = mycol.find().limit(5)
for result in x:
print(result)
sort
Copy cursor = mycol.find().sort([('roll',1)])
for result in cursor:
print(f"Name: {result['name']}, Roll: {result['roll']}")
ফিল্টার করা :
ফিল্টার করে কোয়েরি করার জন্য কয়েকটি কীওয়ার্ড ব্যবহার করা হয়।
Operation
Syntax
Description
Matches values that are equal to a specified value.
Matches values that are less than a specified value.
Matches values that are greater than a specified value.
Matches values that are less than or equal to a specified value.
Matches values that are greater than or equal to a specified value.
Matches all values that are not equal to a specified value.
{ “$and”:[{exp1}, {exp2}, …, {expN}] }
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
{ “$or”:[{exp1}, {<exp2}, …, {expN}] }
Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
{ “$not”:[{exp1}, {exp2}, …, {expN}] }
Inverts the effect of a query expression and returns documents that do not match the query expression.
Equality :
Copy cursor = mycol.find({"name":"trek"})
for record in cursor:
print(record)
Not Equal To
Copy # Not Equal To
cursor = mycol.find({'name':{"$ne":"Mim"}})
for record in cursor:
print(record)
Less Than
Copy # Less Than
cursor = mycol.find({"roll":{'$lt':25}})
for record in cursor:
print(record)
Greater Than
Copy # Greater Than
cursor = mycol.find({"roll":{"$gt":20}})
for record in cursor:
print(record)
IN
Copy cursor = mycol.find({'name': {'$in': ['Mim', 'Mahmuda']}})
for record in cursor:
print(record)
AND
Copy cursor = mycol.find({'$and': [{'name': 'Mim'}, {'roll': 6}]})
for record in cursor:
print(record)
OR
Copy cursor = mycol.find({'$or': [{'name': 'Mim'}, {'roll': 7}]})
for record in cursor:
print(record)
আপডেট রও :
Copy 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.")
ডিলেট রো :
Copy 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.")
ডিলেট ডকুমেন্ট :
Copy 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.")