■ Collection 클래스의 count_documents 메소드를 사용해 컬렉션의 문서 수를 구하는 방법을 보여준다.
▶ 예제 코드 (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 29 30 31 |
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) userCount1 = userCollection.count_documents({}) userCount2 = userCollection.count_documents({"password" : {"$gt" : "password2"}}) print(userCount1) print(userCount2) |
▶ requirements.txt
1 2 3 4 |
dnspython==2.6.1 pymongo==4.7.2 |