0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python3: MongoDB のデータを更新 (Update)

Posted at

プログラム

フォルダー構造

$ tree -a
.
├── .env
├── config_mariadb.py
└── mongo_update.py
mongo_update.py
#! /usr/bin/python
#
#	mongo_update.py
#
#					Mar/01/2023
#
import	sys
import	pymongo
import	datetime
#
from config_mariadb import config_mariadb_proc
# -------------------------------------------------------------------
def	mongo_update_proc (db_aa,collection,key_in,population_in):
	date_mod = datetime.datetime.now ()
	db_aa[collection].update_one({"key": key_in},{"$set":{"population": population_in,"date_mod": '%s' % date_mod}})
#
# -------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
population_in = int(sys.argv[2])
sys.stderr.write("%s\t%d\n" % (key_in, population_in))
#
host,user,password,data_base = config_mariadb_proc()
collection='saitama'
#
client = pymongo.MongoClient(host,username=user,password=password,
		authSource=data_base,authMechanism='SCRAM-SHA-256')
db = client[data_base]
#
mongo_update_proc(db,collection,key_in,population_in)
#
sys.stderr.write("*** 終了 ***\n")
# -------------------------------------------------------------------

.env
config_mariadb.py
はこちら
Python3: MongoDB のデータを作成 (Create)

実行結果

$ ./mongo_update.py t1163 459268700
*** 開始 ***
t1163	459268700
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?