🔥 컬럼명 변경 : '$rename'
🔥 정렬하여 출력하기 : sort()
🔥 해당 key 유무에 따른 document 출력 : '$exists'
🔥 필드값 범위로 출력 : 비교연산자 사용하기
🔥 필드값 범위로 출력 : 논리연산자 사용하기(and, or)
✍🏻 python
import pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection # 컬럼명 변경 actor_collection.update_many( {}, {'$rename': {'기존 컬럼명':'변경할 컬러명'}} )
✍🏻 python
import pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection # sort 문법 docs = actor_collection.find({}).sort('생년월일', pymongo.DESCENDING).limit(10) for doc in docs: print(doc)
✍🏻 python
import pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection docs = actor_collection.find( {"특기": { "$exists": True }} ).limit(5) for doc in docs: print(doc)
- ✔️ 특기가 있는 배우들 중에 흥행지수가 높은 사람부터 5명까지 출력
✍🏻 pythonimport pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection docs = actor_collection.find( {"특기": { "$exists": True }} ).sort('흥행지수', pymongo.DESCENDING).limit(5) for doc in docs: print(doc)
- ✔️ 특기가 없는 배우를 모두 찾은 뒤, 배우이름만 출력
✍🏻 pythonimport pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection docs = actor_collection.find( {"특기": { "$exists": False }}, {'배우이름':1, '_id':0} ) for doc in docs: print(doc)
✍🏻 python
import pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection docs = actor_collection.find( {"흥행지수": { "$gte": 10000 }}, {"배우이름":1,"출연영화":1,"_id":0} ) for doc in docs: print(doc)
✍🏻 python
import pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection docs = actor_collection.find( { '흥행지수':{'$gte':10000}, '출연영화':'신세계' }, {'배우이름':1,'출연영화':1,'_id':0} ) for doc in docs: print(doc)
✍🏻 python
import pymongo # mongodb connection conn = pymongo.MongoClient() # pymongo로 mongodb 연결(localhost:27017) actor_db = conn.cine21 actor_collection = actor_db.actor_collection docs = actor_collection.find( { '$or': [{'출연영화':'신세계'}, {'출연영화':'사바하'}] }, {'배우이름':1,'_id':0} ) for doc in docs: print(doc)