Hướng dẫn python script to load data into mongodb - tập lệnh python để tải dữ liệu vào mongodb

Đ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.

Chèn dữ liệu

Trước tiên chúng tôi sẽ chèn dữ liệu trong MongoDB.

  • Bước 1 - Thiết lập kết nối: Số cổng Mặc định: 27017: Port number Default: 27017
conn = MongoClient(‘localhost’, port-number)
  • Nếu sử dụng số cổng mặc định, tức là 27017. Phương thức kết nối thay thế:
conn = MongoClient()
  • Bước 2 - Tạo cơ sở dữ liệu hoặc chuyển sang cơ sở dữ liệu hiện có:
db = conn.dabasename
  • Tạo một bộ sưu tập hoặc chuyển sang bộ sưu tập hiện có:
collection = db.collection_name
  • Bước 3 - Chèn: Để chèn dữ liệu, hãy tạo một đối tượng từ điển và chèn dữ liệu vào cơ sở dữ liệu. Phương thức được sử dụng để chèn dữ liệu:To Insert Data create a dictionary object and insert data in database. Method used to insert data:
 insert_one() or insert_many()
  • Sau khi chèn để tìm các tài liệu bên trong một bộ sưu tập chúng tôi sử dụng lệnh Find (). Phương thức Find () đưa ra một truy vấn để truy xuất dữ liệu từ một bộ sưu tập trong MongoDB. Tất cả các truy vấn trong MongoDB có phạm vi của một bộ sưu tập duy nhất. Lưu ý: ‘_id, khác nhau cho mọi mục nhập trong bộ sưu tập cơ sở dữ liệu. Hãy cho chúng tôi hiểu chèn dữ liệu với trợ giúp trên mã:-& nbsp;

Python3

from pymongo

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.dabasename
0
db = conn.dabasename
1
db = conn.dabasename
2

db = conn.dabasename
3
db = conn.dabasename
4

conn = MongoClient()
4
conn = MongoClient()
9
db = conn.dabasename
0
db = conn.dabasename
8
db = conn.dabasename
2

collection = db.collection_name
0
conn = MongoClient()
6
collection = db.collection_name
2

collection = db.collection_name
3
conn = MongoClient()
6
collection = db.collection_name
5

collection = db.collection_name
6
conn = MongoClient()
6
collection = db.collection_name
8

collection = db.collection_name
9
 insert_one() or insert_many()
0
conn = MongoClient()
3
 insert_one() or insert_many()
2
 insert_one() or insert_many()
3

collection = db.collection_name
9
 insert_one() or insert_many()
5
conn = MongoClient()
3
 insert_one() or insert_many()
7
 insert_one() or insert_many()
3

collection = db.collection_name
9
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_name
9
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_name
8

collection = db.collection_name
9
 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_name
9
 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_name
9
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_name
9
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.dabasename
0from2from3from4from5

from6

conn = MongoClient()
6 from8

from9 pymongo 0pymongo 1 pymongo 2

conn = MongoClient()
4
conn = MongoClient()
9pymongo 5

  • Output:
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

from pymongo

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.dabasename
0
db = conn.dabasename
1
db = conn.dabasename
2

db = conn.dabasename
3
db = conn.dabasename
4

collection = db.collection_name
0
conn = MongoClient()
6
collection = db.collection_name
2

collection = db.collection_name
0
conn = MongoClient()
6
collection = db.collection_name
2

collection = db.collection_name
3
conn = MongoClient()
6
collection = db.collection_name
5

collection = db.collection_name
6
conn = MongoClient()
6
collection = db.collection_name
8

collection = db.collection_name
9
collection = db.collection_name
8
 insert_one() or insert_many()
5
conn = MongoClient()
3
 insert_one() or insert_many()
7
conn = MongoClient()
32

collection = db.collection_name
9
collection = db.collection_name
8

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_name
9
db = conn.dabasename
2

conn = MongoClient()
9
db = conn.dabasename
0
conn = MongoClient()
57
conn = MongoClient()
58

from6

conn = MongoClient()
6 from8

from9 pymongo 0pymongo 1 pymongo 2

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.dabasename
0
db = conn.dabasename
8
db = conn.dabasename
2


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.