変更したいコレクションが分かっていると思います。
その後、sys.argvまたはコマンドライン引数を解析するために特別に書かれたライブラリを使用してコマンドライン引数を取得できます。 Python 3標準ライブラリにはargpase(https://docs.python.org/3/library/argparse.html)が含まれています。しかし、私はクリック(http://click.pocoo.org/5/)を使用することをお勧めします。
保存のようなコマンドを実行し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()
として以下:あなたはpymongoを持っているし、インストールをクリックしてください
python cli.py --db=test --col=test --build_type=staging --sta
tus=finished
:
pip install pymongo click
おかげで男を。それはうまくいっている。 – user3504250