어떤 컬렉션을 수정하고 싶은지 알고 있다고 생각합니다. 이렇게하면 컬렉션에 명령의 다른 인수로 컬렉션을 추가 할 수 있습니다.
그런 다음 sys.argv 또는 명령 줄 인수를 구문 분석하도록 특별히 작성된 라이브러리를 사용하여 명령 줄 인수를 가져올 수 있습니다. Python 3 표준 라이브러리에는 argpase (https://docs.python.org/3/library/argparse.html)가 포함되어 있습니다. 그러나 클릭 (http://click.pocoo.org/5/)을 사용하는 것이 좋습니다. 당신이 pymongo을 가지고 있는지 확인
python cli.py --db=test --col=test --build_type=staging --sta
tus=finished
설치를 클릭 :
저장 cli.py로 다음은
import click
from pymongo import MongoClient
MONGOHOST = 'localhost'
MONGOPORT = 27017
@click.command()
@click.option('--db', help='Database', required=True)
@click.option('--col', help='Collection', required=True)
@click.option('--build_type', help='Build Type', required=True)
@click.option('--status', help='Status', required=True)
def update(db, col, build_type, status):
mongocol = MongoClient(MONGOHOST, MONGOPORT)[db][col]
mongocol.insert_one({'build_type': build_type, 'status': status})
# You could also do: mongocol.find_and_modify() or whatever...
if __name__ == '__main__':
update()
다음과 같은 명령을 실행
pip install pymongo click
감사 남자. 운동하자. – user3504250