■ Collection 클래스의 insert_many 메소드를 사용해 복수 문서들을 추가하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from pymongo import MongoClient databaseURL = "mongodb://localhost:27017/" mongoClient = MongoClient(databaseURL) testDatabase = mongoClient["testdb"] userCollection = testDatabase["user"] userCollection.delete_many({}) userList = [] for i in range(1, 6): user = { "email" : f"user{i}@sample.com", "password" : f"password{i}" } userList.append(user) userCollection.insert_many(userList) for user in userCollection.find(): print(user) |
▶ requirements.txt
1 2 3 4 |
dnspython==2.6.1 pymongo==4.7.2 |