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 のデータを読む (Read)

Posted at

プログラム

フォルダー構造

$ tree -a
.
├── .env
├── config_mariadb.py
└── mongo_read.py
mongo_read.py
#! /usr/bin/python
#
#	mongo_read.py
#
#					Mar/01/2023
import sys
import pymongo
#
from config_mariadb import config_mariadb_proc
# -------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
#
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]
# db = client.city
collection = db[collection]
#
for data in collection.find():
#	print(data)
	str_out = data["key"] + "\t" + data["name"] + "\t"
	str_out += str(data["population"]) + "\t" + data["date_mod"]
	print(str_out)
#
sys.stderr.write ("*** 終了 ***\n")
# -------------------------------------------------------------------

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

実行結果

$ ./mongo_read.py
*** 開始 ***
t1161	さいたま	52913	2003-8-20
t1162	所沢	17465	2003-2-10
t1163	越谷	91654	2003-7-14
t1164	久喜	53672	2003-9-9
t1165	熊谷	42391	2003-8-4
t1166	秩父	35187	2003-1-21
t1167	川越	81946	2003-7-23
t1168	和光	29714	2003-10-16
t1169	新座	41823	2003-12-25
*** 終了 ***
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?