Điều kiện tiên quyết: MongoDB Python Basics trước tiên chúng ta sẽ hiểu cách chèn một tài liệu/mục nhập vào bộ sưu tập cơ sở dữ liệu. Sau đó, chúng tôi sẽ làm việc về cách cập nhật một tài liệu hiện có trong MongoDB bằng thư viện Pymongo trong Python. Các lệnh cập nhật giúp chúng tôi cập nhật dữ liệu truy vấn được chèn vào bộ sưu tập cơ sở dữ liệu MongoDB. MongoDB Python Basics We would first understand how to insert a document/entry in a collection of a database. Then we would work on how to update an existing document in MongoDB using pymongo library in python. The update commands helps us to update the query data inserted already in MongoDB database collection. Show Chèn dữ liệu Trước tiên chúng tôi sẽ chèn dữ liệu trong MongoDB.
conn = MongoClient(‘localhost’, port-number)
conn = MongoClient()
db = conn.dabasename
collection = db.collection_name
insert_one() or insert_many()
Python3
conn = MongoClient()0 conn = MongoClient()1 conn = MongoClient()2 conn = MongoClient()3 conn = MongoClient()4 conn = MongoClient()5 conn = MongoClient()6 conn = MongoClient()7 conn = MongoClient()4 conn = MongoClient()9 db = conn.dabasename0 db = conn.dabasename1 db = conn.dabasename2 db = conn.dabasename3 db = conn.dabasename4 conn = MongoClient()4 conn = MongoClient()9 db = conn.dabasename0 db = conn.dabasename8 db = conn.dabasename2 collection = db.collection_name0 conn = MongoClient()6 collection = db.collection_name2 collection = db.collection_name3 conn = MongoClient()6 collection = db.collection_name5 collection = db.collection_name6 conn = MongoClient()6 collection = db.collection_name8 collection = db.collection_name9 insert_one() or insert_many()0 conn = MongoClient()3 insert_one() or insert_many()2 insert_one() or insert_many()3 collection = db.collection_name9 insert_one() or insert_many()5 conn = MongoClient()3 insert_one() or insert_many()7 insert_one() or insert_many()3 collection = db.collection_name9 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}0 conn = MongoClient()3 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}2 collection = db.collection_name9 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}4 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}5 conn = MongoClient()6 collection = db.collection_name8 collection = db.collection_name9 insert_one() or insert_many()0 conn = MongoClient()3 Connected successfully!!! Data updated with id {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geeksforgeeks', 'eid': 24, 'location': 'delhi', 'lastModified': datetime.datetime(2017, 11, 7, 21, 19, 9, 698000)} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}1 insert_one() or insert_many()3 collection = db.collection_name9 insert_one() or insert_many()5 conn = MongoClient()3 Connected successfully!!! Data updated with id {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geeksforgeeks', 'eid': 24, 'location': 'delhi', 'lastModified': datetime.datetime(2017, 11, 7, 21, 19, 9, 698000)} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}6 insert_one() or insert_many()3 collection = db.collection_name9 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}0 conn = MongoClient()3 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}2 collection = db.collection_name9 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}4 print(result.matched_count)4 conn = MongoClient()6 print(result.matched_count)6 print(result.matched_count)7 conn = MongoClient()6 print(result.matched_count)9 conn = MongoClient()9 db = conn.dabasename0 from 2from 3from 4from 5
conn = MongoClient()6 from 8
conn = MongoClient()4 conn = MongoClient()9 pymongo 5
Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'} Cập nhật dữ liệu trong MongoDBB Các phương thức được sử dụng: Update_one () và update_many () tham số được truyền: + một tài liệu bộ lọc để khớp với các tài liệu để cập nhật + một tài liệu cập nhật để chỉ định sửa đổi để thực hiện + tham số UPSERT tùy chọn sau khi chèn dữ liệu vào MongoDB Hãy cập nhật dữ liệu của nhân viên với nhân viên với nhân viên ID: 24 & nbsp; Parameters passed: + a filter document to match the documents to update + an update document to specify the modification to perform + an optional upsert parameter After inserting Data in MongoDB let’s Update the Data of employee with id:24 Python3
conn = MongoClient()0 conn = MongoClient()1 conn = MongoClient()2 conn = MongoClient()3 conn = MongoClient()4 conn = MongoClient()5 conn = MongoClient()6 conn = MongoClient()7 conn = MongoClient()4 conn = MongoClient()9 db = conn.dabasename0 db = conn.dabasename1 db = conn.dabasename2 db = conn.dabasename3 db = conn.dabasename4 collection = db.collection_name0 conn = MongoClient()6 collection = db.collection_name2 collection = db.collection_name0 conn = MongoClient()6 collection = db.collection_name2 collection = db.collection_name3 conn = MongoClient()6 collection = db.collection_name5 collection = db.collection_name6 conn = MongoClient()6 collection = db.collection_name8 collection = db.collection_name9 collection = db.collection_name8 insert_one() or insert_many()5 conn = MongoClient()3 insert_one() or insert_many()7 conn = MongoClient()32 collection = db.collection_name9 collection = db.collection_name8 conn = MongoClient()35 conn = MongoClient()36 conn = MongoClient()37 conn = MongoClient()38 insert_one() or insert_many()0 conn = MongoClient()3 conn = MongoClient()41 conn = MongoClient()38 conn = MongoClient()32 conn = MongoClient()35 conn = MongoClient()45 conn = MongoClient()37 conn = MongoClient()47 conn = MongoClient()3 conn = MongoClient()49 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}4 conn = MongoClient()35 Connected successfully!!! Data inserted with record ids {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geek', 'eid': 24, 'location': 'delhi'} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'}4 collection = db.collection_name9 db = conn.dabasename2 conn = MongoClient()9 db = conn.dabasename0 conn = MongoClient()57 conn = MongoClient()58
conn = MongoClient()6 from 8
Cập nhật dữ liệu trong MongoDB Output: Connected successfully!!! Data updated with id {'_id': ObjectId('5a02227b37b8552becf5ed2a'), 'name': 'Mr.Geeksforgeeks', 'eid': 24, 'location': 'delhi', 'lastModified': datetime.datetime(2017, 11, 7, 21, 19, 9, 698000)} {'_id': ObjectId('5a02227c37b8552becf5ed2b'), 'name': 'Mr.Shaurya', 'eid': 14, 'location': 'delhi'} Các phương thức được sử dụng: Update_one () và update_many () tham số được truyền: + một tài liệu bộ lọc để khớp với các tài liệu để cập nhật + một tài liệu cập nhật để chỉ định sửa đổi để thực hiện + tham số UPSERT tùy chọn sau khi chèn dữ liệu vào MongoDB Hãy cập nhật dữ liệu của nhân viên với nhân viên với nhân viên ID: 24 & nbsp; print(result.matched_count) conn = MongoClient()4 conn = MongoClient()9 db = conn.dabasename0 db = conn.dabasename8 db = conn.dabasename2 Làm cách nào để nhập tệp CSV vào MongoDB bằng Python?Nhập tệp CSV vào MongoDB bằng Python.. Mongoclient = Mongoclient () db = Mongoclient. Tháng Mười_Mug_Talk db. .... csvfile = open ('nhân viên.csv', 'r') reader = csv. .... Đối với mỗi người đọc: hàng = {} cho trường trong tiêu đề: hàng [trường] = mỗi [trường]. Nhập CSV từ pymongo nhập Mongoclient Mongoclient = Mongoclient () db = Mongoclient .. Python có thể kết nối với MongoDB không?Pymongo, thư viện trình điều khiển mongoDB tiêu chuẩn cho Python, rất dễ sử dụng và cung cấp API trực quan để truy cập cơ sở dữ liệu, bộ sưu tập và tài liệu.Các đối tượng được lấy từ MongoDB qua Pymongo tương thích với từ điển và danh sách, vì vậy chúng ta có thể dễ dàng thao tác, lặp lại và in chúng., is easy to use and offers an intuitive API for accessing databases, collections, and documents. Objects retrieved from MongoDB through PyMongo are compatible with dictionaries and lists, so we can easily manipulate, iterate, and print them.
Làm thế nào để Python lưu trữ dữ liệu JSON trong MongoDB?Các bước sau đây để thao tác dữ liệu từ JSON đến MongoDB Python được liệt kê dưới đây ... Bước 1: Tạo một bộ sưu tập bằng pymongo .. Bước 2: Chèn dữ liệu từ JSON đến MongoDB Python .. Bước 3: Lọc dữ liệu .. Bước 4: Lưu dữ liệu từ MongoDB vào JSON Python .. Làm thế nào thủ công dữ liệu trong MongoDB?Để chèn dữ liệu vào Bộ sưu tập MongoDB, bạn cần sử dụng phương thức Chèn () hoặc lưu () của MongoDB.use MongoDB's insert() or save() method. |