<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MongoDB | Men of Letters（メン・オブ・レターズ） &#8211; 論理的思考/業務改善/プログラミング</title>
	<atom:link href="https://laboratory.kazuuu.net/category/mongodb/feed/" rel="self" type="application/rss+xml" />
	<link>https://laboratory.kazuuu.net</link>
	<description></description>
	<lastBuildDate>Thu, 23 Mar 2023 20:40:51 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://laboratory.kazuuu.net/wp-content/uploads/2021/02/cropped-L-32x32.png</url>
	<title>MongoDB | Men of Letters（メン・オブ・レターズ） &#8211; 論理的思考/業務改善/プログラミング</title>
	<link>https://laboratory.kazuuu.net</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>PythonでGridFSとPyMongoを用いてMongoDBに画像を保存する</title>
		<link>https://laboratory.kazuuu.net/storing-images-in-mongodb-using-gridfs-and-pymongo-in-python/</link>
					<comments>https://laboratory.kazuuu.net/storing-images-in-mongodb-using-gridfs-and-pymongo-in-python/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Thu, 23 Mar 2023 20:40:51 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=13699</guid>

					<description><![CDATA[PythonでGridFSとPyMongoを用いてMongoDBに画像を保存してみます。 なお、今回はPyMongoモジュールを用います。このライブラリ・モジュールはPythonの標準ライブラリではありませんので、事前に [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>PythonでGridFSとPyMongoを用いてMongoDBに画像を保存してみます。</p>
<p>なお、今回はPyMongoモジュールを用います。このライブラリ・モジュールはPythonの標準ライブラリではありませんので、事前にインストールする必要があります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■Python</a></li><li><a href="#toc2" tabindex="0">■仮想環境の構築</a></li><li><a href="#toc3" tabindex="0">■仮想環境への切り替え</a></li><li><a href="#toc4" tabindex="0">■pymongoのインストール</a></li><li><a href="#toc5" tabindex="0">■画像を用意する</a></li><li><a href="#toc6" tabindex="0">■GridFSとPyMongoを用いてMongoDBに画像を保存する</a><ol><li><a href="#toc7" tabindex="0">■コード</a></li></ol></li><li><a href="#toc8" tabindex="0">■実行・検証</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■Python</span></h2>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1.png"><img fetchpriority="high" decoding="async" class="alignnone size-medium wp-image-13581" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-300x168.png" alt="" width="300" height="168" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-300x168.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-1024x573.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-768x429.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-120x68.png 120w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-160x90.png 160w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1-320x180.png 320w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-03-124034-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回のPythonのバージョンは、「Python 3.9.13」を使用しています。（Windows10）</p>
<h2><span id="toc2">■仮想環境の構築</span></h2>
<p>GridFSとPyMongoを用いてMongoDBに画像を保存してみますが、その前に仮想環境の構築を行い、インストールを行います。そのために、まずはWindows10のコマンドプロンプトを起動します。</p>
<pre>C:\Users\user_&gt;mkdir mongo_image</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを押します。「mkdir」コマンドで「mongo_image」ディレクトリを作成します。</p>
<pre>C:\Users\user_&gt;cd mongo_image</pre>
<p>作成後、上記のコマンドを入力し、Enterキーを押します。「cd」コマンドで「mongo_image」ディレクトリ内に移動します。</p>
<pre>C:\Users\user_\mongo_image&gt;py -3.9 -m venv .venv</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを押します。本来だと「python -m venv .venv」で仮想環境のディレクトリ「.venv」を作成しますが、今回はPythonのバージョンを指定しています。なお、今回仮想環境のディレクトリ名は「.venv」としていますが、ご自身で変更は可能です。</p>
<p>Enterキーを押すと、何も出力されませんが、これで仮想環境の作成が完了となります。</p>
<h2><span id="toc3">■仮想環境への切り替え</span></h2>
<pre>C:\Users\user_\mongo_image&gt;.venv\Scripts\activate.bat</pre>
<p>完了後、上記のコマンドを入力し、Enterキーを押します。仮想環境のディレクトリ内に作成された activateスクリプトを実行し、仮想環境に入ります。</p>
<pre>(.venv) C:\Users\user_\mongo_image&gt;</pre>
<p>Enterキーを押すと、「(.venv)」と表示されます。これで仮想環境に入ることができました。</p>
<h2><span id="toc4">■pymongoのインストール</span></h2>
<pre>(.venv) C:\Users\user_\mongo_image&gt;pip install pymongo</pre>
<p>仮想環境に入った後に、上記のコマンドを入力し、Enterキーを押します。これでpipを経由してpymongoをインストールします。</p>
<pre>Collecting pymongo
  Using cached pymongo-4.2.0-cp39-cp39-win_amd64.whl (374 kB)
Installing collected packages: pymongo
Successfully installed pymongo-4.2.0
WARNING: You are using pip version 22.0.4; however, version 22.2.2 is available.
You should consider upgrading via the 'C:\Users\user_\mongo_image\.venv\Scripts\python.exe -m pip install --upgrade pip' command.</pre>
<p>Enterキーを押すと、インストールが開始され、「Successfully installed（正常にインストールされました）」と出力されます。これが出力されればインストールは完了となります。「WARNING」も出力されているが、警告であってエラーではないので、今回は一旦無視します。</p>
<h2><span id="toc5">■画像を用意する</span></h2>
<p>pymongoをインストール後、MongoDBに画像を保存するため、画像を用意します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10-084639-1.png"><img decoding="async" class="alignnone size-medium wp-image-13700" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10-084639-1-300x113.png" alt="" width="300" height="113" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10-084639-1-300x113.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10-084639-1-1024x384.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10-084639-1-768x288.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10-084639-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回は「C:\Users\user_\mongo_image（フォルダパス）」内に「mountain.png」というpng形式の画像ファイルを用意しました。この画像ファイルは、フリーで高品質な画像・動画素材を提供している「Pixabay（<a href="https://pixabay.com/ja/">https://pixabay.com/ja/</a>）」から著作権フリーの画像を利用しています。</p>
<h2><span id="toc6">■GridFSとPyMongoを用いてMongoDBに画像を保存する</span></h2>
<p>画像の用意ができましたので、GridFSとPyMongoを用いてMongoDBに画像を保存するスクリプトを書いていきます。</p>
<h3><span id="toc7">■コード</span></h3>
<pre>from pymongo import MongoClient 
import gridfs

connection = MongoClient("mongodb+srv://kazu:un43ynGWuPaYVQ6@cluster0.cywla.mongodb.net/?retryWrites=true&amp;w=majority")

database = connection['test_db']

fs = gridfs.GridFS(database)

img_file = r"C:\Users\user_\mongo_image\mountain.png"

with open(img_file,'rb') as f:
    contents = f.read()

fs.put(contents,filename="mountani_image")</pre>
<p>今回は「from import」でpymongoのMongoClientを呼び出します。さらにimportでgridfsも呼び出します。</p>
<p>その後、「connection = MongoClient()」を用いて、MongoClientを使用しMongoDBへの接続を作成します。括弧内には、今回MongoDB Cloud（<a href="https://cloud.mongodb.com/">https://cloud.mongodb.com/</a>）にログインを行い、「DEPLOYMENT」の「Database」で、事前に作成した「Cluster 0」というクラスターを選択し、「Connect」ボタンをクリックする。クリックすると、「Connect to Cluster0」ウインドウが表示され、「Connect your application」を選択。選択後、「Select your driver and version」で「DRIVER」を「Python」と選択し、「VERSION」を今回は「3.6 or later」に選択する。選択すると、「Add your connection string into your application code」にコードが表示されるので、このコードをコピーし引数,パラメータとして渡す。渡す際に「mongodb+srv://username:&lt;password&gt;@cluster0.cywla.mongodb.net/?retryWrites=true&amp;w=majority」の「&lt;password&gt;」の部分をDatabaseのパスワードに置き換えて引数,パラメータとして渡す。</p>
<p>MongoDBへの接続を作成した後に、databaseという変数を定義し、その中に「connection[&#8216;test_db&#8217;]」と記述する。今回は事前に作成した「Cluster 0」というクラスター内に「test_db」というデータベースと、「collection01」というコレクションを作成しているので、「[&#8216;test_db&#8217;]」としている。これでデータベースが指定される。</p>
<p>その後、fsという変数を定義し、その中でgridfs.GridFS()を用います。括弧内には、database変数を引数,パラメータとして渡します。これでGridFSの新しいインスタンスが作成されます。</p>
<p>作成後、img_fileという変数を定義し、その中に今回用意した画像ファイルの名前を置かれている場所を格納します。</p>
<p>格納後、with構文とopen()を用います。open()の括弧内の第1の引数,パラメータとしてimg_file変数を渡します。次に第2の引数,パラメータとして&#8221;rb&#8221;を渡します。これで開いたものをバイナリファイルとして読み込むモードになります。バイナリファイルとして読み込んだものをf変数に格納します。その後、contentsという変数を定義し、その中にf変数に対してread()を用います。これで格納されたものを読み込んでcontents変数へ格納します。</p>
<p>最後にfs.put()を用います。括弧内には第1の引数,パラメータとしてcontents変数を渡します。次に第2の引数,パラメータとしてfilenameを渡します。これでデータを新しいファイルとしてGridFSに入れます。GridFSは、画像、オーディオ ファイル、ビデオファイルなどの大きなファイルを格納および取得するためのMongoDBの仕様です。</p>
<p>このスクリプトを、「mongo_image」ディレクトリ内に「db_imagefile.py」という名前で保存します。</p>
<h2><span id="toc8">■実行・検証</span></h2>
<p>保存後、コマンドプロンプト上で仮想環境に入った状態でスクリプトファイルを実行してみます。</p>
<pre>(.venv) C:\Users\user_\mongo_image&gt;python db_imagefile.py</pre>
<pre>Traceback (most recent call last):
  File "C:\Users\user_\mongo_image\db_imagefile.py", line 4, in 
    connection = MongoClient("mongodb+srv://kazu:un43ynGWuPaYVQ6@cluster0.cywla.mongodb.net/?retryWrites=true&amp;w=majority")
  File "C:\Users\user_\mongo_image\.venv\lib\site-packages\pymongo\mongo_client.py", line 726, in __init__
    res = uri_parser.parse_uri(
  File "C:\Users\user_\mongo_image\.venv\lib\site-packages\pymongo\uri_parser.py", line 469, in parse_uri
    raise ConfigurationError(
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs. To fix this error install pymongo with the srv extra:
 C:\Users\user_\mongo_image\.venv\Scripts\python.exe -m pip install "pymongo[srv]"</pre>
<p>スクリプトを実行してみると、「pymongo.errors.ConfigurationError」というエラーが発生しました。エラー内容を確認すると、dnspythonモジュールがインストールされていないことが原因と判明。</p>
<pre>(.venv) C:\Users\user_\mongo_image&gt;pip install "pymongo[srv]"</pre>
<p>判明後、上記のコマンドを入力し、Enterキーを押します。これでdnspythonモジュールがインストールされます。</p>
<pre>Requirement already satisfied: pymongo[srv] in c:\users\user_\mongo_image\.venv\lib\site-packages (4.2.0)
Collecting dnspython&lt;3.0.0,&gt;=1.16.0
Using cached dnspython-2.2.1-py3-none-any.whl (269 kB)
Installing collected packages: dnspython
Successfully installed dnspython-2.2.1</pre>
<p>Enterキーを押すと、インストールが開始され、「Successfully installed（正常にインストールされました）」と出力されます。これが出力されればdnspythonのインストールは完了となります。</p>
<pre>(.venv) C:\Users\user_\mongo_image&gt;python db_imagefile.py</pre>
<p>完了後、再度上記のコマンドを入力し、Enterキーを押してみます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_1-1.png"><img decoding="async" class="alignnone size-medium wp-image-13702" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_1-1-300x160.png" alt="" width="300" height="160" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_1-1-300x160.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_1-1-1024x546.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_1-1-768x410.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_1-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、何も出力されませんが、先程のエラーは出力されず、MongoDBに画像が保存されました。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13703" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-300x170.png" alt="" width="300" height="170" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-300x170.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-1024x582.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-768x436.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-120x68.png 120w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-160x90.png 160w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1-320x180.png 320w, https://laboratory.kazuuu.net/wp-content/uploads/2022/08/スクリーンショット-2022-08-10_2-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>保存後、MongoDB Cloud（<a href="https://cloud.mongodb.com/">https://cloud.mongodb.com/</a>）にログインを行い、「DEPLOYMENT」の「Database」で、事前に作成した「Cluster 0」というクラスターを選択し、「Browse Collections」ボタンをクリックします。クリックすると、Database内のCollectionsなどを確認できますが、この中に「fs.files」と「fs.chunks」という2つのコレクションが追加されていることが確認できました。</p>
<p>この2つのコレクションは、画像ファイルのメタデータとチャンクが格納されています。各チャンクは、一意の「_id ObjectId」フィールドによって識別されています。「fs.files」のコレクションは親ドキュメントとして機能し、「fs.chunks」のコレクションの「files_id」フィールドは、チャンクを親である「fs.files」にリンクしています。なおチャンクとは、キー範囲とそのキー範囲が関連するシャードを記述する小さなレコードです。シャードとは、複数のマシンにデータを分散または分割する方法です。</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/storing-images-in-mongodb-using-gridfs-and-pymongo-in-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MongoDB shellのデータベース操作</title>
		<link>https://laboratory.kazuuu.net/mongodb-shell-database-operations/</link>
					<comments>https://laboratory.kazuuu.net/mongodb-shell-database-operations/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Wed, 25 Jan 2023 20:44:45 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=13211</guid>

					<description><![CDATA[MongoDB shellのデータベース操作について解説しています。 目次 ■PC環境■MongoDB shellの起動■データベースの一覧を表示する■データベースの作成■コレクションの作成■コレクションにドキュメントを [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>MongoDB shellのデータベース操作について解説しています。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-4" checked><label class="toc-title" for="toc-checkbox-4">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■MongoDB shellの起動</a></li><li><a href="#toc3" tabindex="0">■データベースの一覧を表示する</a></li><li><a href="#toc4" tabindex="0">■データベースの作成</a></li><li><a href="#toc5" tabindex="0">■コレクションの作成</a></li><li><a href="#toc6" tabindex="0">■コレクションにドキュメントを追加する</a></li><li><a href="#toc7" tabindex="0">■追加したドキュメントを全件取得する</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>Windows10（Google Chrome）</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>AWS インスタンス作成済み（Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)）</p>
<p>MongoDB 5.0 Community Edition インストール済み</p>
<p>MongoDB shell version v5.0.9</p>
<h2><span id="toc2">■MongoDB shellの起動</span></h2>
<pre>&gt; mongo</pre>
<p>上記のコマンドを入力し、Enterキーを押します。</p>
<h2><span id="toc3">■データベースの一覧を表示する</span></h2>
<pre>&gt; show dbs</pre>
<p>上記のコマンドを入力し、Enterキーを押します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130332-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13212" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130332-1-300x131.png" alt="" width="300" height="131" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130332-1-300x131.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130332-1-1024x448.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130332-1-768x336.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130332-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、MongoDBサーバー上のすべてのデータベースを一覧で出力する。出力された一覧には、データベース名とデータベースのサイズがギガバイト単位で表示される。</p>
<h2><span id="toc4">■データベースの作成</span></h2>
<pre>&gt; use データベースの名前</pre>
<p>データベースの作成のためには、上記のコマンドを入力し、Enterキーを押す。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130723-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13213" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130723-1-300x143.png" alt="" width="300" height="143" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130723-1-300x143.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130723-1-1024x488.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130723-1-768x366.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-14-130723-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回は検証のため、データベースの名前を「test_1」としEnterキーを押す。</p>
<pre>switched to db データベース名</pre>
<p>Enterキーを押すと、上記のメッセージが出力される。「switched（切り替え）」と表示されているため、作成されたデータベースに切り替えが行われる。なお、データベースの名前は英数字を使用し、64文字未満である必要がある。なお、英語の大文字と小文字が区別され、データベースの名前を空にすることはできない（ヌル文字、スペース文字、または空の文字列も使用できない）。さらにWindows OSの場合「/ \ . ” $ * &lt; &gt; : | ?」といった特殊文字が使えず、Linux/Unix OSの場合「/ \ . ” $」といった特殊文字が使えない。</p>
<h2><span id="toc5">■コレクションの作成</span></h2>
<p>作成したデータベースの続きとなるが、次にコレクションの作成を行う。このコレクションは、MySQLなどのデータベースにみられる「テーブル」というものに類似している。MongoDBは、最初のドキュメントの挿入すると、自動的にコレクションの作成を行う。そのため、コレクションを作成する必要がない場合もある。</p>
<pre>&gt; db.createCollection("コレクションの名前")</pre>
<p>コレクションの作成するために、上記のコマンドを入力し、Enterキーを押す。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084658-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13218" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084658-1-300x42.png" alt="" width="300" height="42" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084658-1-300x42.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084658-1-1024x143.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084658-1-768x108.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084658-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>{ "ok" : 1 }</pre>
<p>Enterキーを押すと、上記のように出力される。今回は「test_1」というコレクションを作成。コレクションの名前をつける場合には、キャメルケースまたは小文字の名前を使用します。また、空の文字列や”$”、ヌル文字、System （システム生成名）という接頭辞といった特殊文字でコレクションの名前を始めることができない。なお、コレクションの名前のスペースの最大長は120バイト以下となっている。</p>
<h2><span id="toc6">■コレクションにドキュメントを追加する</span></h2>
<p>作成したコレクションの続きとなるが、コレクションにドキュメントを追加する。</p>
<pre>&gt; db.collection_name.insert( { name:'test', sex:'male' } );</pre>
<p>コレクションにドキュメントを追加するために、上記のコマンドを入力し、Enterキーを押す。今回は「test_1」というコレクション内に上記のキー（Key）と値（value）のペアのセット（ドキュメント）を追加するので、「collection_name」を「test_1」に変更する。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084805-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13219" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084805-1-300x49.png" alt="" width="300" height="49" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084805-1-300x49.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084805-1-1024x167.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084805-1-768x126.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-084805-1.png 1248w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>WriteResult({ "nInserted" : 1 })</pre>
<p>Enterキーを押すと、上記のようなメッセージが出力される。これが出力されれば、コレクション内にドキュメントが追加される。</p>
<h2><span id="toc7">■追加したドキュメントを全件取得する</span></h2>
<p>ドキュメントを追加した続きとなるが、追加したドキュメントを全件取得し、MongoDB shellで表示する。</p>
<pre>&gt; db.collection_name.find()</pre>
<p>追加したドキュメントを全件取得するために、上記のコマンドを入力し、Enterキーを押す。今回は「test_1」というコレクション内のドキュメント）を全件取得するので「collection_name」を「test_1」に変更する。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-085410-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13220" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-085410-1-300x68.png" alt="" width="300" height="68" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-085410-1-300x68.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-085410-1-1024x233.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-085410-1-768x175.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-15-085410-1.png 1248w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、コレクション内に追加したドキュメントが取得される。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/mongodb-shell-database-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Ubuntu 22.04 LTS上でインストールしたMongoDBサービスの開始とMongoDB Shellの起動</title>
		<link>https://laboratory.kazuuu.net/starting-mongodb-service-and-mongodb-shell-installed-on-ubuntu-22-04-lts/</link>
					<comments>https://laboratory.kazuuu.net/starting-mongodb-service-and-mongodb-shell-installed-on-ubuntu-22-04-lts/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Tue, 17 Jan 2023 20:45:29 +0000</pubDate>
				<category><![CDATA[Amazon/AWS]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=13160</guid>

					<description><![CDATA[Ubuntu 22.04 LTS上でインストールしたMongoDBサービスの開始とMongoDB Shellの起動を行ってみます。 なお、この記事は「Ubuntu 22.04 LTS上でMongoDB 5.0 Commu [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Ubuntu 22.04 LTS上でインストールしたMongoDBサービスの開始とMongoDB Shellの起動を行ってみます。</p>
<p>なお、この記事は「<a href="https://laboratory.kazuuu.net/installing-mongodb-5-0-community-edition-on-ubuntu-22-04-lts">Ubuntu 22.04 LTS上でMongoDB 5.0 Community Editionをインストールする</a>」という記事の続きです。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-6" checked><label class="toc-title" for="toc-checkbox-6">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■SSHクライアント「PuTTY」を用いてSSHで接続する</a></li><li><a href="#toc2" tabindex="0">■MongoDB 5.0 Community Editionのサービス開始</a></li><li><a href="#toc3" tabindex="0">■MongoDB Shellの起動</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■SSHクライアント「PuTTY」を用いてSSHで接続する</span></h2>
<p>Ubuntu 22.04 LTS上でインストールしたMongoDBサービスの開始とMongoDB Shellの起動を行う前に、まず、「AWS マネジメントコンソール」にログインし、「EC2」サービスへ移動します。移動後、「EC2 Management Console」内の左メニューの「インスタンス」の「インスタンス」をクリックすると、作成した「インスタンス」が一覧で表示されます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13099" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-300x72.png" alt="" width="300" height="72" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-300x72.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-1024x244.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-768x183.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回作成した「Ubuntu Server」インスタンスの「インスタンス状態」が「実行中」であることを確認します。確認後、SSHクライアント「PuTTY」を用いて、SSHで接続を行います。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13094" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-300x187.png" alt="" width="300" height="187" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-300x187.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-1024x640.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-768x480.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13095" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-300x58.png" alt="" width="300" height="58" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-300x58.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-1024x197.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-768x147.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)
* Documentation: https://help.ub 
* Management: https://landscape.canonical.com 
* Support: https://ubuntu.com/advantage
System information as of Fri Jul 8 00:49:50 UTC 2022
System load: 0.0 Usage of 7: 22.6% of 7.58GB Memory usage: 234 Swap usage: 0%
Processes:
100 Users logged in: IPv4 address for etho: ***.**.**.**
* Ubuntu Pro delivers the most comprehensive open source security and,
compliance features.

<a rel="noopener" href="https://ubuntu.com/aws/pro" title="Ubuntu Pro for AWS | Ubuntu" class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://assets.ubuntu.com/v1/47f12466-og_%20ubuntu.png" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Ubuntu Pro for AWS | Ubuntu</div><div class="blogcard-snippet external-blogcard-snippet">Ubuntu Pro for AWS, the Ubuntu image optimised for production and professional use on public cloud. Including broad secu...</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://ubuntu.com/aws/pro" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">ubuntu.com</div></div></div></div></a>

42 updates can be applied immediately. 
22 of these updates are standard security updates. 
To see these additional updates run: apt list --upgradable</pre>
<p>SSHで接続すると、上記のようなメッセージが出力されます。</p>
<pre>ubuntu@ip-**-**-**-**(ipアドレス):~$</pre>
<p>出力後、上記のように出力されれば、接続は完了となります。</p>
<h2><span id="toc2">■MongoDB 5.0 Community Editionのサービス開始</span></h2>
<pre>$ sudo systemctl start mongod.service</pre>
<p>接続後、インストールしたMongoDB 5.0 Community Editionのサービスを開始します。開始するために、上記のコマンドを入力し、Enterキーを押します。「systemctl」コマンドを用いてMongoDBサービスを開始します。systemctlは、広く採用されているinitシステムとサービスマネージャーsystemdの制御インターフェイスおよび検査ツールです。systemdは、Linux初期化システムおよびサービスマネージャーであり、デーモン（バックグラウンドで実行される通常のプログラム）のオンデマンド起動、マウントおよび自動マウントポイントのメンテナンスなどの機能を含みます。Enterキーを押すと何も出力されませんが、MongoDBサービスが開始されます。</p>
<pre>$ sudo systemctl status mongod</pre>
<p>開始後、実際にMongoDBサービスが開始されたのかを確認してみます。確認の際には上記のコマンドを入力し、Enterキーを押します。「systemctl」コマンドを用いて「mongod」の「status（状態）」を確認します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-12-083511-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13169" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-12-083511-1-300x117.png" alt="" width="300" height="117" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-12-083511-1-300x117.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-12-083511-1-1024x401.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-12-083511-1-768x301.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-12-083511-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>??ongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor prese&gt;
     Active: active (running) since Mon 2022-07-11 23:32:08 UTC; 2min 9s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 27971 (mongod)
     Memory: 66.9M
        CPU: 1.221s
     CGroup: /system.slice/mongod.service
             ??27971 /usr/bin/mongod --config /etc/mongod.conf

Jul 11 23:32:08 ip-***-**-**-*** systemd[1]: Started MongoDB Database Server.</pre>
<p>Enterキーを押すと、「MongoDB Database Server（mongod）」の状態が出力されます。出力内容を確認すると、「Active: active (running)（アクティブ（動作中））」と書かれていますので、サービスが開始されていることが確認できました。</p>
<h2><span id="toc3">■MongoDB Shellの起動</span></h2>
<pre>$ mongosh</pre>
<p>確認後、MongoDB Shellの起動を行います。起動するために、上記のコマンドを入力し、Enterキーを押します。このコマンドでMongoDB Shellの起動を行います。</p>
<pre>Current Mongosh Log ID: **********
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&amp;serverSelectionTimeoutMS=2000&amp;appName=mongosh+1.5.0
Using MongoDB:          5.0.9
Using Mongosh:          1.5.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2022-07-11T23:32:08.717+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2022-07-11T23:32:09.475+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

------
   Enable MongoDB's free cloud-based monitoring service, which will then receive and display
   metrics about your deployment (disk utilization, CPU, operation statistics, etc).

   The monitoring data will be available on a MongoDB website with a unique URL accessible to you
   and anyone you share the URL with. MongoDB may use this information to make product
   improvements and to suggest MongoDB products and deployment options to you.

   To enable free monitoring, run the following command: db.enableFreeMonitoring()
   To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------

test&gt;</pre>
<p>Enterキーを押すと、上記のメッセージが出力されます。これでMongoDB Shellの起動は完了となります。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/starting-mongodb-service-and-mongodb-shell-installed-on-ubuntu-22-04-lts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Ubuntu 22.04 LTS上でMongoDBのパッケージを確認する</title>
		<link>https://laboratory.kazuuu.net/checking-mongodb-packages-on-ubuntu-22-04-lts/</link>
					<comments>https://laboratory.kazuuu.net/checking-mongodb-packages-on-ubuntu-22-04-lts/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Mon, 16 Jan 2023 20:46:19 +0000</pubDate>
				<category><![CDATA[Amazon/AWS]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=13138</guid>

					<description><![CDATA[Ubuntu 22.04 LTS上でMongoDBのパッケージを確認してみます。なお、確認するにあたり、Sudo特権ユーザーアカウントを用います。 なお、この記事は「AWS EC2の「Ubuntu Server」インスタ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Ubuntu 22.04 LTS上でMongoDBのパッケージを確認してみます。なお、確認するにあたり、Sudo特権ユーザーアカウントを用います。</p>
<p>なお、この記事は「<a href="https://laboratory.kazuuu.net/connect-to-an-aws-ec2-ubuntu-server-instance-via-ssh-using-the-ssh-client-putty">AWS EC2の「Ubuntu Server」インスタンスにSSHクライアント「PuTTY」を用いてSSHで接続する</a>」という記事の続きでもあります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-8" checked><label class="toc-title" for="toc-checkbox-8">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■SSHクライアント「PuTTY」を用いてSSHで接続する</a></li><li><a href="#toc3" tabindex="0">■MongoDBのパッケージを確認する</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>Windows10（Google Chrome）</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>AWS インスタンス作成済み（Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)）</p>
<h2><span id="toc2">■SSHクライアント「PuTTY」を用いてSSHで接続する</span></h2>
<p>MongoDBのパッケージを確認する前に、まず、「AWS マネジメントコンソール」にログインし、「EC2」サービスへ移動します。移動後、「EC2 Management Console」内の左メニューの「インスタンス」の「インスタンス」をクリックすると、作成した「インスタンス」が一覧で表示されます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13099" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-300x72.png" alt="" width="300" height="72" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-300x72.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-1024x244.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-768x183.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回作成した「Ubuntu Server」インスタンスの「インスタンス状態」が「実行中」であることを確認します。確認後、SSHクライアント「PuTTY」を用いて、SSHで接続を行います。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13094" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-300x187.png" alt="" width="300" height="187" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-300x187.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-1024x640.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-768x480.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13095" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-300x58.png" alt="" width="300" height="58" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-300x58.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-1024x197.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-768x147.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)
* Documentation: https://help.ub 
* Management: https://landscape.canonical.com 
* Support: https://ubuntu.com/advantage
System information as of Fri Jul 8 00:49:50 UTC 2022
System load: 0.0 Usage of 7: 22.6% of 7.58GB Memory usage: 234 Swap usage: 0%
Processes:
100 Users logged in: IPv4 address for etho: ***.**.**.**
* Ubuntu Pro delivers the most comprehensive open source security and,
compliance features.

<a rel="noopener" href="https://ubuntu.com/aws/pro" title="Ubuntu Pro for AWS | Ubuntu" class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://assets.ubuntu.com/v1/47f12466-og_%20ubuntu.png" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Ubuntu Pro for AWS | Ubuntu</div><div class="blogcard-snippet external-blogcard-snippet">Ubuntu Pro for AWS, the Ubuntu image optimised for production and professional use on public cloud. Including broad secu...</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://ubuntu.com/aws/pro" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">ubuntu.com</div></div></div></div></a>

42 updates can be applied immediately. 
22 of these updates are standard security updates. 
To see these additional updates run: apt list --upgradable</pre>
<p>SSHで接続すると、上記のようなメッセージが出力されます。</p>
<pre>ubuntu@ip-**-**-**-**(ipアドレス):~$</pre>
<p>出力後、上記のように出力されれば、接続は完了となります。</p>
<h2><span id="toc3">■MongoDBのパッケージを確認する</span></h2>
<pre>$ sudo apt show mongodb-org</pre>
<p>接続後、MongoDBのパッケージを確認します。確認するために、上記のコマンドを入力し、Enterキーを押します。sudo権限を用いて「apt show」コマンドでmongodb-orgのパッケージを確認します。</p>
<pre>Package: mongodb-org
State: not a real package (virtual)
N: Can't select candidate version from package mongodb as it has no candidate
N: Can't select versions from package 'mongodb-org' as it is purely virtual
N: No packages found</pre>
<p>Enterキーを押すと、今回は上記のようなメッセージが出力されました。出力された内容を確認すると、「State: not a real package (virtual)（状態：実際のパッケージではない（仮想））」と書かれていますので、今回はmongodb-orgのパッケージはインストールされていないことが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/checking-mongodb-packages-on-ubuntu-22-04-lts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Ubuntu 22.04 LTS上でMongoDB 5.0 Community Editionをインストールする</title>
		<link>https://laboratory.kazuuu.net/installing-mongodb-5-0-community-edition-on-ubuntu-22-04-lts/</link>
					<comments>https://laboratory.kazuuu.net/installing-mongodb-5-0-community-edition-on-ubuntu-22-04-lts/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Sun, 15 Jan 2023 20:40:01 +0000</pubDate>
				<category><![CDATA[Amazon/AWS]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=13142</guid>

					<description><![CDATA[Ubuntu 22.04 LTS上でMongoDB 5.0 Community Editionをインストールしてみます。 なお、この記事は「AWS EC2の「Ubuntu Server」インスタンスにSSHクライアント「 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Ubuntu 22.04 LTS上でMongoDB 5.0 Community Editionをインストールしてみます。</p>
<p>なお、この記事は「<a href="https://laboratory.kazuuu.net/connect-to-an-aws-ec2-ubuntu-server-instance-via-ssh-using-the-ssh-client-putty">AWS EC2の「Ubuntu Server」インスタンスにSSHクライアント「PuTTY」を用いてSSHで接続する</a>」という記事の続きでもあります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-10" checked><label class="toc-title" for="toc-checkbox-10">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■SSHクライアント「PuTTY」を用いてSSHで接続する</a></li><li><a href="#toc3" tabindex="0">■MongoDB 5.0 Community Editionのパッケージを確認する</a></li><li><a href="#toc4" tabindex="0">■インストールの確認</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>Windows10（Google Chrome）</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>AWS インスタンス作成済み（Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)）</p>
<h2><span id="toc2">■SSHクライアント「PuTTY」を用いてSSHで接続する</span></h2>
<p>MongoDB 5.0 Community Editionをインストールしてみます。まず、その前に「AWS マネジメントコンソール」にログインし、「EC2」サービスへ移動します。移動後、「EC2 Management Console」内の左メニューの「インスタンス」の「インスタンス」をクリックすると、作成した「インスタンス」が一覧で表示されます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13099" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-300x72.png" alt="" width="300" height="72" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-300x72.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-1024x244.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1-768x183.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-120355-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回作成した「Ubuntu Server」インスタンスの「インスタンス状態」が「実行中」であることを確認します。確認後、SSHクライアント「PuTTY」を用いて、SSHで接続を行います。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13094" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-300x187.png" alt="" width="300" height="187" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-300x187.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-1024x640.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1-768x480.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095006-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-13095" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-300x58.png" alt="" width="300" height="58" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-300x58.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-1024x197.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1-768x147.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-08-095019-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)
* Documentation: https://help.ub 
* Management: https://landscape.canonical.com 
* Support: https://ubuntu.com/advantage
System information as of Fri Jul 8 00:49:50 UTC 2022
System load: 0.0 Usage of 7: 22.6% of 7.58GB Memory usage: 234 Swap usage: 0%
Processes:
100 Users logged in: IPv4 address for etho: ***.**.**.**
* Ubuntu Pro delivers the most comprehensive open source security and,
compliance features.

<a rel="noopener" href="https://ubuntu.com/aws/pro" title="Ubuntu Pro for AWS | Ubuntu" class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://assets.ubuntu.com/v1/47f12466-og_%20ubuntu.png" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Ubuntu Pro for AWS | Ubuntu</div><div class="blogcard-snippet external-blogcard-snippet">Ubuntu Pro for AWS, the Ubuntu image optimised for production and professional use on public cloud. Including broad secu...</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://ubuntu.com/aws/pro" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">ubuntu.com</div></div></div></div></a>

42 updates can be applied immediately. 
22 of these updates are standard security updates. 
To see these additional updates run: apt list --upgradable</pre>
<p>SSHで接続すると、上記のようなメッセージが出力されます。</p>
<pre>ubuntu@ip-**-**-**-**(ipアドレス):~$</pre>
<p>出力後、上記のように出力されれば、接続は完了となります。</p>
<h2><span id="toc3">■MongoDB 5.0 Community Editionのパッケージを確認する</span></h2>
<p>接続後、MongoDB 5.0 Community Editionをインストールします。</p>
<pre>$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -</pre>
<p>インストールするために、まずは上記のコマンドを入力し、Enterキーを押します。「wget」コマンドを用いて、MongoDBパブリックGPG(GNU Privacy Guard,公開鍵)キーを<a href="https://www.mongodb.org/static/pgp/server-5.0.asc">https://www.mongodb.org/static/pgp/server-5.0.asc</a>からインポートします。</p>
<pre>Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK</pre>
<p>Enterキーを押すと、「Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).（警告: apt-key は非推奨です。代わりに trusted.gpg.d でキーリングのファイルを管理します (apt-key(8) を参照)。）」というメッセージが出力されます。これは警告でありエラーではないので、そのままインストールの作業を進めます。</p>
<pre>$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list</pre>
<p>MongoDBパブリックGPG(GNU Privacy Guard,公開鍵)キーをインポート後、上記のコマンドを入力し、Enterキーを押します。これでMongoDBのリストファイルを作成します。</p>
<pre>deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse</pre>
<p>Enterキーを押すと、上記のように表示されます。これで「sources.list.d」ディレクトリに「mongodb-org-5.0.list」というファイルが作成されます。</p>
<pre>$ sudo apt-get update</pre>
<p>作成後、上記のコマンドを入力し、Enterキーを押します。sudo権限で、ローカルパッケージのデータベースをリロードします。Enterキーを押すと、リロードが開始され、しばらくすると完了となります。</p>
<pre>$ sudo apt-get install -y mongodb-org</pre>
<p>リロード後、MongoDBパッケージの最新の安定バージョンをインストールします。インストールするために、上記のコマンドを入力し、Enterキーを押します。</p>
<pre>Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mongodb-org-mongos : Depends: libssl1.1 (&gt;= 1.1.1) but it is not installable
 mongodb-org-server : Depends: libssl1.1 (&gt;= 1.1.1) but it is not installable
 mongodb-org-shell : Depends: libssl1.1 (&gt;= 1.1.1) but it is not installable
E: Unable to correct problems, you have held broken packages.</pre>
<p>Enterキーを押すと、「Some packages could not be installed.（いくつかのパッケージはインストールできませんでした。）」と出力され、「E: Unable to correct problems, you have held broken packages.（E: 問題を修正できません。壊れたパッケージを保持しています。）」というエラーが発生し、インストールができない。インストールできない原因を調べてみると、Ubuntu 22.04ではlibsslを3にアップグレードしてしまうので、libsslのバージョン1.1にする必要があるということで、下記の各コマンドを入力し、Enterキーを押していく（<a href="https://askubuntu.com/questions/1403619/mongodb-install-fails-on-ubuntu-22-04-depends-on-libssl1-1-but-it-is-not-insta">https://askubuntu.com/questions/1403619/mongodb-install-fails-on-ubuntu-22-04-depends-on-libssl1-1-but-it-is-not-insta</a>）。</p>
<pre>echo "deb http://security.ubuntu.com/ubuntu impish-security main" | sudo tee /etc/apt/sources.list.d/impish-security.list

sudo apt-get update
sudo apt-get install libssl1.1</pre>
<p>Enterキーを押していくと「libssl1.1」のインストールが完了となります。</p>
<pre>$ sudo apt-get install -y mongodb-org</pre>
<p>完了後、再度、MongoDBパッケージのインストールするために、上記のコマンドを入力し、Enterキーを押します。Enterキーを押すと、今度はインストールが開始され、しばらくすると完了となります。</p>
<h2><span id="toc4">■インストールの確認</span></h2>
<pre>$ sudo apt show mongodb-org</pre>
<p>インストール完了後、実際にインストールされているかの確認のために、上記のコマンドを入力し、Enterキーを押します。</p>
<pre>Package: mongodb-org
Version: 5.0.9
Priority: optional
Section: database
Maintainer: MongoDB Packaging &lt;packaging@mongodb.com&gt;
Installed-Size: 8192 B
Depends: mongodb-org-database, mongodb-org-tools, mongodb-mongosh
Conflicts: mongo-10gen, mongo-10gen-enterprise, mongo-10gen-enterprise-server, mongo-10gen-server, mongo-10gen-unstable, mongo-10gen-unstable-enterprise, mongo-10gen-unstable-enterprise-mongos, mongo-10gen-unstable-enterprise-server, mongo-10gen-unstable-enterprise-shell, mongo-10gen-unstable-enterprise-tools, mongo-10gen-unstable-mongos, mongo-10gen-unstable-server, mongo-10gen-unstable-shell, mongo-10gen-unstable-tools, mongo18-10gen, mongo18-10gen-server, mongo20-10gen, mongo20-10gen-server, mongodb, mongodb-10gen, mongodb-10gen-enterprise, mongodb-10gen-unstable, mongodb-10gen-unstable-enterprise, mongodb-10gen-unstable-enterprise-mongos, mongodb-10gen-unstable-enterprise-server, mongodb-10gen-unstable-enterprise-shell, mongodb-10gen-unstable-enterprise-tools, mongodb-10gen-unstable-mongos, mongodb-10gen-unstable-server, mongodb-10gen-unstable-shell, mongodb-10gen-unstable-tools, mongodb-clients, mongodb-dev, mongodb-enterprise, mongodb-enterprise-mongos, mongodb-enterprise-server, mongodb-enterprise-shell, mongodb-enterprise-tools, mongodb-enterprise-unstable, mongodb-enterprise-unstable-mongos, mongodb-enterprise-unstable-server, mongodb-enterprise-unstable-shell, mongodb-enterprise-unstable-tools, mongodb-nightly, mongodb-org-unstable, mongodb-org-unstable-mongos, mongodb-org-unstable-server, mongodb-org-unstable-shell, mongodb-org-unstable-tools, mongodb-server, mongodb-stable, mongodb18-10gen, mongodb20-10gen
Homepage: http://www.mongodb.org
Download-Size: 2932 B
APT-Manual-Installed: yes
APT-Sources: https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 Packages
Description: MongoDB open source document-oriented database system (metapackage)
 MongoDB is built for scalability, performance and high availability, scaling from single server deployments to large, complex multi-site architectures. By leveraging in-memory computing, MongoDB provides high performance for both reads and writes. MongoDB?s native replication and automated failover enable enterprise-grade reliability and operational flexibility.
 .
 MongoDB is an open-source database used by companies of all sizes, across all industries and for a wide variety of applications. It is an agile database that allows schemas to change quickly as applications evolve, while still providing the functionality developers expect from traditional databases, such as secondary indexes, a full query language and strict consistency.
 .
 MongoDB has a rich client ecosystem including hadoop integration, officially supported drivers for 10 programming languages and environments, as well as 40 drivers supported by the user community.
 .
 MongoDB features:
 * JSON Data Model with Dynamic Schemas
 * Auto-Sharding for Horizontal Scalability
 * Built-In Replication for High Availability
 * Rich Secondary Indexes, including geospatial
 * TTL indexes
 * Text Search
 * Aggregation Framework &amp; Native MapReduce
 .
 This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.

N: There are 9 additional records. Please use the '-a' switch to see them.</pre>
<p>Enterキーを押すと、「Package: mongodb-org」,「Version: 5.0.9」などの情報が出力されました。出力内容からMongoDB 5.0 Community Editionがインストールされていることが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/installing-mongodb-5-0-community-edition-on-ubuntu-22-04-lts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>DockerでMongoDBコンテナーと対話しコレクションにドキュメントを作成し検索する</title>
		<link>https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-and-search-documents-in-collections/</link>
					<comments>https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-and-search-documents-in-collections/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Fri, 30 Dec 2022 20:43:57 +0000</pubDate>
				<category><![CDATA[Docker（ドッカー）]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12970</guid>

					<description><![CDATA[DockerでMongoDBコンテナーと対話しコレクションにドキュメントを作成し検索してみます。 この記事は、前回公開した「DockerでMongoDBコンテナーと対話し新しいデータベースとコレクションを作成する」という [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>DockerでMongoDBコンテナーと対話しコレクションにドキュメントを作成し検索してみます。</p>
<p>この記事は、前回公開した「<a href="https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-new-databases-and-collections">DockerでMongoDBコンテナーと対話し新しいデータベースとコレクションを作成する</a>」という記事の続きになります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-12" checked><label class="toc-title" for="toc-checkbox-12">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■環境</a></li><li><a href="#toc2" tabindex="0">■save()を用いてコレクションにドキュメントを追加する</a></li><li><a href="#toc3" tabindex="0">■insert()を用いてドキュメントを追加する</a></li><li><a href="#toc4" tabindex="0">■コレクション内の特定のドキュメントを検索する</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■環境</span></h2>
<p>Windows10（DockerDesktopインストール済み）</p>
<p>docker version</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12437" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png" alt="" width="300" height="287" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-1024x981.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-768x735.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Client:
Cloud integration: v1.0.22
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:44:07 2021
OS/Arch: windows/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:43:56 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0</pre>
<h2><span id="toc2">■save()を用いてコレクションにドキュメントを追加する</span></h2>
<p>前回「crazy_napier」という新しいMongoDBコンテナーに接続し、「test-db」という新しいデータベースを作成し、「user」というコレクションを追加しました。</p>
<pre>&gt; use test-db
switched to db test-db
&gt; db.user.save({ name:"田中" })</pre>
<p>追加を行いましたので、コレクション内にドキュメントを追加してみます。「use」コマンドを用いてtest-dbのデータベースに切り替わっている（switched to）かの確認を行い、上記のコマンドを入力し、Enterキーを押します。dbの「user」というコレクションに対してsave()を用います。括弧内には引数,パラメータとしてJSON形式（キー（名前）とバリュー（値）のペア）を渡します。今回はキーが「name」で、バリューが「田中」とします。</p>
<pre>WriteResult({ "nInserted" : 1 })</pre>
<p>Enterキーを押すと、上記のメッセージが出力されます。これでコレクション内にドキュメントが追加されました。</p>
<pre>&gt; db.user.find()</pre>
<p>追加後、実際に追加されたのかの確認を行います。確認のために、上記のコマンドを入力し、Enterキーを押します。userというコレクション内のドキュメントを取得し表示します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_6-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12971" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_6-1-300x108.png" alt="" width="300" height="108" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_6-1-300x108.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_6-1-1024x367.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_6-1-768x275.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_6-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、コレクション内のドキュメントが表示され、今回追加したドキュメントが表示されていることが確認できました。この時、キーと値だけでなくカーソルIDも表示される。なお、今回はバリュー（値）に日本語を用いましたが日本語であっても追加できることもわかりました。</p>
<h2><span id="toc3">■insert()を用いてドキュメントを追加する</span></h2>
<pre>&gt;db.user.insert({name:"松本"});</pre>
<p>確認後、insert()を用いてもドキュメントを追加することができるのでやってみます。MongoDBシェル上で、上記のコマンドを入力し、Enterキーを押します。userというコレクションに対してinsert()を用います。括弧内には引数,パラメータとして、先程同じくJSON形式（キー（名前）とバリュー（値）のペア）を渡します。</p>
<pre>WriteResult({ "nInserted" : 1 })</pre>
<p>Enterキーを押すと、上記のメッセージが出力されます。これでコレクション内にドキュメントが追加されました。</p>
<pre>&gt;db.user.find()</pre>
<p>追加後、追加されたのかの確認を行います。確認のために、上記のコマンドを入力し、Enterキーを押します。先程同じくfind()を用います。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_7-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12972" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_7-1-300x84.png" alt="" width="300" height="84" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_7-1-300x84.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_7-1-1024x285.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_7-1-768x214.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_7-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、コレクション内のドキュメントが表示され、insert()を用いて追加したドキュメントが確認できました。</p>
<h2><span id="toc4">■コレクション内の特定のドキュメントを検索する</span></h2>
<pre>&gt;db.user.find({name:"田中"});</pre>
<p>確認後、コレクション内の特定のドキュメントを検索してみます。検索するために、上記のコマンドを入力し、Enterキーを押します。先程同じくfind()を用いますが、今回は括弧内に引数,パラメータとして検索するドキュメントの値を渡します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_8-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12973" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_8-1-300x77.png" alt="" width="300" height="77" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_8-1-300x77.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_8-1-1024x264.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_8-1-768x198.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-04_8-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、引数,パラメータとして渡した特定のドキュメントが検索され、結果が出力されました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-and-search-documents-in-collections/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>DockerでMongoDBコンテナーと対話し新しいデータベースとコレクションを作成する</title>
		<link>https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-new-databases-and-collections/</link>
					<comments>https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-new-databases-and-collections/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Fri, 23 Dec 2022 20:44:27 +0000</pubDate>
				<category><![CDATA[Docker（ドッカー）]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12947</guid>

					<description><![CDATA[DockerでMongoDBコンテナーとの対話を行ってみます。その結果として新しいデータベースとコレクションを作成してみます。 この記事は以前公開した「DockerでMongoDBコンテナーをセットアップ（設定）する」と [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>DockerでMongoDBコンテナーとの対話を行ってみます。その結果として新しいデータベースとコレクションを作成してみます。</p>
<p>この記事は以前公開した「<a href="https://laboratory.kazuuu.net/setup-a-mongodb-container-with-docker">DockerでMongoDBコンテナーをセットアップ（設定）する</a>」という記事の続きとなります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-14" checked><label class="toc-title" for="toc-checkbox-14">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■環境</a></li><li><a href="#toc2" tabindex="0">■MongoDBコンテナーに接続する</a></li><li><a href="#toc3" tabindex="0">■新しいデータベースを作成する</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■環境</span></h2>
<p>Windows10（DockerDesktopインストール済み）</p>
<p>docker version</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12437" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png" alt="" width="300" height="287" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-1024x981.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-768x735.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Client:
Cloud integration: v1.0.22
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:44:07 2021
OS/Arch: windows/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:43:56 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0</pre>
<h2><span id="toc2">■MongoDBコンテナーに接続する</span></h2>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_2-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12948" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_2-1-300x180.png" alt="" width="300" height="180" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_2-1-300x180.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_2-1-1024x614.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_2-1-768x461.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_2-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>DockerでMongoDBコンテナーとの対話を行う前に、MongoDBコンテナーに接続してみます。なお、今回は「crazy_napier」という新しいMongoDBコンテナーを作り、それと対話します。</p>
<pre>&gt;docker exec -it crazy_napier bash</pre>
<p>接続のために、Windows10のコマンドプロンプトを起動します。「docker exec」コマンドでcrazy_napierという稼働中のコンテナに対して、コマンドを実行します。それは、「-it」オプションで、bashプロセスを新規で立ち上げ、操作できるようにするものです。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_3-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12949" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_3-1-300x140.png" alt="" width="300" height="140" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_3-1-300x140.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_3-1-1024x479.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_3-1-768x359.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_3-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、bashシェルが立ち上がります。</p>
<pre>&gt;mongo</pre>
<p>立ち上がった後に、上記のコマンドを入力し、Enterキーを押します。これでMongoDBシェルクライアントを起動できます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12950" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1-300x172.png" alt="" width="300" height="172" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1-300x172.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1-1024x586.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1-768x439.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1-120x68.png 120w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_4-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&amp;gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("*****-****-****-****-****") }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see

<a rel="noopener" href="https://docs.mongodb.com/mongodb-shell/install/" title="Install mongosh - mongosh - MongoDB Docs" class="blogcard-wrap external-blogcard-wrap a-wrap cf" target="_blank"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img loading="lazy" decoding="async" src="https://laboratory.kazuuu.net/wp-content/uploads/cocoon-resources/blog-card-cache/45d726028ac76cd94e79e601ab11bcef.png" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="160" height="90" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Install mongosh - mongosh - MongoDB Docs</div><div class="blogcard-snippet external-blogcard-snippet">Prerequisites, compatibility issues, and installation instructions for mongosh, the MongoDB Shell.</div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img loading="lazy" decoding="async" src="https://www.google.com/s2/favicons?domain=https://docs.mongodb.com/mongodb-shell/install/" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">docs.mongodb.com</div></div></div></div></a>
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting:
        2022-07-03T05:18:49.463+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2022-07-03T05:18:50.414+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-07-03T05:18:50.414+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---</pre>
<p>Enterキーを押すと、上記のメッセージが出力され、MongoDBシェルクライアントが起動します。</p>
<h2><span id="toc3">■新しいデータベースを作成する</span></h2>
<pre>&gt;use test-db</pre>
<p>MongoDBシェルクライアントを起動後、上記のコマンドを入力し、Enterキーを押します。「use」コマンドを使用し、test-dbという新しいデータベースを作成してみます。</p>
<pre>switched to db test-db</pre>
<p>Enterキーを押すと、「switched to db test-db（db”test-db”に切り替えました）」と出力され、新しいデータベースが作成され、作成したデータベースに切り替えが行われました。</p>
<pre>&gt;show dbs;</pre>
<p>切り替え後、上記のコマンドを入力し、Enterキーを押します。これでデータベースの一覧を表示します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_5-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12951" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_5-1-300x204.png" alt="" width="300" height="204" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_5-1-300x204.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_5-1-1024x697.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_5-1-768x523.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_5-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、データベースの一覧が表示されますが、今回作成したtest-dbという新しいデータベースは確認できませんでした。確認できるようにするためには、コレクションを作成する必要があります。</p>
<pre>&gt;db.createCollection('user');</pre>
<p>コレクションを作成する必要があるので、上記のコマンドを入力し、Enterキーを押します。これでデータベース内にコレクションを作成します。</p>
<pre>{ "ok" : 1 }</pre>
<p>Enterキーを押すと、上記のメッセージが出力されます。これでコレクションの作成が完了となります。</p>
<pre>&gt;show dbs;</pre>
<p>作成完了後、再度上記のコマンドを入力し、Enterキーを押します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_6-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12952" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_6-1-300x174.png" alt="" width="300" height="174" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_6-1-300x174.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_6-1-1024x595.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_6-1-768x446.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-03_6-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、データベースの一覧が表示されますが、コレクションを追加したため、test-dbというデータベースが確認できる状態となりました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/interact-with-mongodb-containers-in-docker-to-create-new-databases-and-collections/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>DockerでMongoDBコンテナーをセットアップ（設定）する</title>
		<link>https://laboratory.kazuuu.net/setup-a-mongodb-container-with-docker/</link>
					<comments>https://laboratory.kazuuu.net/setup-a-mongodb-container-with-docker/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Tue, 20 Dec 2022 20:44:25 +0000</pubDate>
				<category><![CDATA[Docker（ドッカー）]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12905</guid>

					<description><![CDATA[DockerでMongoDBコンテナーをセットアップ（設定）してみます。 この記事は以前公開した「DockerでMongoDBイメージをダウンロードする」という記事の続きとなります。 目次 ■環境■MongoDBコンテナ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>DockerでMongoDBコンテナーをセットアップ（設定）してみます。</p>
<p>この記事は以前公開した「<a href="https://laboratory.kazuuu.net/download-mongodb-image-with-docker">DockerでMongoDBイメージをダウンロードする</a>」という記事の続きとなります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-16" checked><label class="toc-title" for="toc-checkbox-16">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■環境</a></li><li><a href="#toc2" tabindex="0">■MongoDBコンテナーをセットアップ（設定）</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■環境</span></h2>
<p>Windows10（DockerDesktopインストール済み）</p>
<p>docker version</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12437" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png" alt="" width="300" height="287" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-1024x981.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-768x735.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Client:
Cloud integration: v1.0.22
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:44:07 2021
OS/Arch: windows/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:43:56 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0</pre>
<h2><span id="toc2">■MongoDBコンテナーをセットアップ（設定）</span></h2>
<p>DockerでMongoDBイメージをダウンロードしましたので、次にMongoDBコンテナーをセットアップ（設定）を行っていきます。セットアップするために、Windows10のコマンドプロンプトを起動します。</p>
<pre>&gt;docker run --name test-db-mongo mongo</pre>
<pre>&gt;docker run --name tutorial mongo:4.0.9</pre>
<p>起動した後に、上記のコマンドを入力し、Enterキーを押します。「docker run」で新しいコンテナでコマンドを実行します。実行する時に、MongoDBインスタンスをデプロイし、コンテナー名を付けます。コンテナー名は「&#8211;name」オプションで指定します。今回は「test-db-mongo」というコンテナー名にします。もし、バージョンを指定したい場合は「:”コロン”」を用いてバージョンを指定します。</p>
<p>Enterキーを押すと、コンテナーのセットアップ（設定）できます。</p>
<pre>{"t":{"$date":"2022-07-02T02:57:59.368+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
{"t":{"$date":"2022-07-02T02:57:59.380+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2022-07-02T02:57:59.385+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2022-07-02T02:57:59.386+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2022-07-02T02:57:59.394+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2022-07-02T02:57:59.394+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
{"t":{"$date":"2022-07-02T02:57:59.394+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
{"t":{"$date":"2022-07-02T02:57:59.394+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
{"t":{"$date":"2022-07-02T02:57:59.395+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"6308d9b11b01"}}
{"t":{"$date":"2022-07-02T02:57:59.395+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"5.0.9","gitVersion":"6f7dae919422dcd7f4892c10ff20cdc721ad00e6","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2022-07-02T02:57:59.395+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
{"t":{"$date":"2022-07-02T02:57:59.395+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"}}}}
{"t":{"$date":"2022-07-02T02:57:59.400+00:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
{"t":{"$date":"2022-07-02T02:57:59.401+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=2629M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
{"t":{"$date":"2022-07-02T02:58:00.069+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1656730680:69878][1:0x7fe5030f9c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global recovery timestamp: (0, 0)"}}
{"t":{"$date":"2022-07-02T02:58:00.069+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1656730680:69956][1:0x7fe5030f9c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global oldest timestamp: (0, 0)"}}</pre>
<p>コンテナーのセットアップ（設定）ができますが、上記のようなメッセージが出力され、セットアップ（設定）ができないことがあります。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-122349.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12906" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-122349-300x132.png" alt="" width="300" height="132" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-122349-300x132.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-122349-1024x451.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-122349-768x338.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-122349.png 1224w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>このような場合は、DockerDesktopを起動し開きます。開くとウインドウ内の左メニューに「Images」がありますので、こちらをクリックします。クリックすると、「Images」の一覧が表示されます。表示された中にダウンロードしたMongoDBイメージがあり、その左側に「RUN」ボタンがありますので、こちらのボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_1-1-2.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12907" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_1-1-2-300x138.png" alt="" width="300" height="138" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_1-1-2-300x138.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_1-1-2-1024x470.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_1-1-2-768x353.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_1-1-2.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「New Container」というウインドウが表示されますので、ウインドウ内の「Run」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_2-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12908" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_2-1-300x137.png" alt="" width="300" height="137" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_2-1-300x137.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_2-1-1024x466.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_2-1-768x350.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02_2-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリック後、DockerDesktopの「Containers/Apps」をクリックすると、新しいMongoDBコンテナーが作られていることが確認できました。</p>
<pre>&gt;docker container ls</pre>
<p>Windows10のコマンドプロンプト上でも、上記のコマンドを入力し、Enterキーを押してみます。このコマンドで、稼働中のコンテナ一覧を表示します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-123212-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12909" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-123212-1-300x93.png" alt="" width="300" height="93" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-123212-1-300x93.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-123212-1-1024x319.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-123212-1-768x239.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-02-123212-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、先程の新しいMongoDBコンテナが出力されることが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/setup-a-mongodb-container-with-docker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>DockerでMongoDBイメージをダウンロードする</title>
		<link>https://laboratory.kazuuu.net/download-mongodb-image-with-docker/</link>
					<comments>https://laboratory.kazuuu.net/download-mongodb-image-with-docker/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Thu, 15 Dec 2022 20:45:33 +0000</pubDate>
				<category><![CDATA[Docker（ドッカー）]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12889</guid>

					<description><![CDATA[DockerでMongoDBイメージをダウンロードしてみます。 目次 ■環境■DockerでMongoDBイメージをダウンロード ■環境 Windows10（DockerDesktopインストール済み） docker v [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>DockerでMongoDBイメージをダウンロードしてみます。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-18" checked><label class="toc-title" for="toc-checkbox-18">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■環境</a></li><li><a href="#toc2" tabindex="0">■DockerでMongoDBイメージをダウンロード</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■環境</span></h2>
<p>Windows10（DockerDesktopインストール済み）</p>
<p>docker version</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12437" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png" alt="" width="300" height="287" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-300x287.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-1024x981.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1-768x735.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/05/スクリーンショット-2022-05-23-4-1-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Client:
Cloud integration: v1.0.22
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:44:07 2021
OS/Arch: windows/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:43:56 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0</pre>
<h2><span id="toc2">■DockerでMongoDBイメージをダウンロード</span></h2>
<p>早速DockerでMongoDBイメージをダウンロードします。ダウンロードのために、Windows10のコマンドプロンプトを起動します。</p>
<pre>&gt;docker pull mongo</pre>
<pre>&gt;docker pull mongo:4.0.9</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを押します。「docker pull」コマンドを用いて、レジストリからMongoDBイメージを取得します。特定のバージョンをダウンロードする場合は「コロン”:”」の後にバージョンを指定し、Enterキーを押します。</p>
<pre>Using default tag: latest
latest: Pulling from library/mongo
d7bfe07ed847: Pull complete
97ef66a8492a: Pull complete
20cec14c8f9e: Pull complete
38c3018eb09a: Pull complete
ccc9e1c2556b: Pull complete
593c62d03532: Pull complete
1a103a446c3f: Pull complete
be887b845d3f: Pull complete
e5543880b183: Pull complete
Digest: sha256:37e84d3dd30cdfb5472ec42b8a6b4dc6ca7cacd91ebcfa0410a54528bbc5fa6d
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest</pre>
<p>Enterキーを押すと、MongoDBイメージのダウンロードが開始され、上記のメッセージが表示されます。これが表示されれば、ダウンロードは完了となります。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-01-133438.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12890" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-01-133438-300x144.png" alt="" width="300" height="144" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-01-133438-300x144.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-01-133438-1024x491.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-01-133438-768x368.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/07/スクリーンショット-2022-07-01-133438.png 1281w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>ダウンロード後、DockerDesktopを起動し、「Images」をクリックすると、「mongo」というイメージがダウンロードされていることが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/download-mongodb-image-with-docker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pythonでpymongoを用いてMongoDBAtlasデータベース内にコレクションを作成しキーと値を挿入する</title>
		<link>https://laboratory.kazuuu.net/creating-a-collection-and-inserting-keys-and-values-in-mongodbatlas-database-using-pymongo-in-python/</link>
					<comments>https://laboratory.kazuuu.net/creating-a-collection-and-inserting-keys-and-values-in-mongodbatlas-database-using-pymongo-in-python/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Sat, 09 Jul 2022 20:43:08 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12061</guid>

					<description><![CDATA[Pythonでpymongoを用いてMongoDBAtlasデータベース内にコレクションを作成しキーと値を挿入してみます。 なお、今回は無料プランでMongoDBAtlasを利用しています。MongoDBAtlasの初期 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Pythonでpymongoを用いてMongoDBAtlasデータベース内にコレクションを作成しキーと値を挿入してみます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-110854-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12062" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-110854-1-300x174.png" alt="" width="300" height="174" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-110854-1-300x174.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-110854-1-1024x593.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-110854-1-768x445.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-110854-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>なお、今回は無料プランでMongoDBAtlasを利用しています。MongoDBAtlasの初期の状態では、「DATABASES」内に「Cluster0」というデータベースがあり、このデータベースにはコレクションは作成されていない状態となっています。</p>
<p>また、今回はpymongoモジュールを用います。このライブラリ・モジュールはPythonの標準ライブラリではありませんので、事前にインストールする必要があります。</p>
<p>なお、MongoDBAtlasに接続する場合は、mongo+srvプロトコルを使用する必要があるため、</p>
<pre>pip install pymongo[srv]</pre>
<p>上記のコマンドでpip経由でpymongo-srvをインストールする。これがないと「dnspython module must be installed to use mongodb+srv:// URI」というエラーが発生し、接続できない。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-20" checked><label class="toc-title" for="toc-checkbox-20">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■Python</a></li><li><a href="#toc2" tabindex="0">■pymongoを用いてMongoDBAtlasデータベース内にコレクションを作成しキーと値を挿入する</a><ol><li><a href="#toc3" tabindex="0">■コード</a></li></ol></li><li><a href="#toc4" tabindex="0">■実行・検証</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■Python</span></h2>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2021/05/スクリーンショット-2021-05-15-072949.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-8343" src="https://laboratory.kazuuu.net/wp-content/uploads/2021/05/スクリーンショット-2021-05-15-072949-300x197.png" alt="" width="300" height="197" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2021/05/スクリーンショット-2021-05-15-072949-300x197.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2021/05/スクリーンショット-2021-05-15-072949-1024x671.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2021/05/スクリーンショット-2021-05-15-072949-768x503.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2021/05/スクリーンショット-2021-05-15-072949.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回のPythonのバージョンは、「3.8.5」を使用しています。（Windows10）（pythonランチャーでの確認）</p>
<h2><span id="toc2">■pymongoを用いてMongoDBAtlasデータベース内にコレクションを作成しキーと値を挿入する</span></h2>
<p>では、早速pymongoを用いてMongoDBAtlasデータベース内にコレクションを作成しキーと値を挿入するスクリプトを書いていきます。</p>
<h3><span id="toc3">■コード</span></h3>
<pre>import pymongo  
client = pymongo.MongoClient("接続するためのコード") 

mydb = client["Cluster0"]
mycol = mydb["test_1"]

mydict = { "ユーザー名": "テスト一太郎", "password": "test12345abf" }

mycol.insert_one(mydict)</pre>
<p>importでpymongoモジュールを呼び出します。その後、clientという変数を定義し、その中でpymongo.MongoClient()を用います。括弧内には、MongoDBAtlasに接続するためのコードが必要となります。<a href="https://laboratory.kazuuu.net/connect-to-mongodbatlas-using-pymongo-in-python">コードの取得に関してはこちらを参考</a>にしてください。</p>
<p>client変数を定義後、mydbという変数を定義し、その中でclientで”[ ]”（角括弧）を用いてデータベースを指定します。今回はCluster0を指定します。</p>
<p>その後、mycolという変数を定義し、その中でmydb変数を指定し”[ ]”（角括弧）を用いてコレクションを指定します。今回はコレクションは作成していないので、新しくtest_1というコレクションの名前を指定し作成します。</p>
<p>次にmydict変数を定義し、その中で”{ }”（波括弧）を用いて辞書（JSON表記）を作成し、辞書内にキーと値を記述します。</p>
<p>最後にmycolに対して、insert_one()を用います。括弧内には引数,パラメータとして、単一のドキュメント（mydict）を渡します。これで辞書内のキーと値が挿入されます。</p>
<h2><span id="toc4">■実行・検証</span></h2>
<p>このスクリプトを「py_m_db_item_add.py」という名前で、Pythonが実行されている作業ディレクトリ（カレントディレクトリ）に保存し、コマンドプロンプトから実行してみます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112342-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12063" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112342-1-300x100.png" alt="" width="300" height="100" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112342-1-300x100.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112342-1-1024x340.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112342-1-768x255.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112342-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<blockquote><p>C:\Users\user_\AppData\Roaming\Python\Python38\site-packages\cryptography\x509\base.py:531: CryptographyDeprecationWarning: Parsed a negative serial number, which is disallowed by RFC 5280.<br />
return rust_x509.load_pem_x509_certificate(data)</p></blockquote>
<p>実行してみると、上記のメッセージが出力されました。エラーではないので、これでデータが挿入されたことになります。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112700-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12064" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112700-1-300x193.png" alt="" width="300" height="193" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112700-1-300x193.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112700-1-1024x659.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112700-1-768x495.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/04/スクリーンショット-2022-04-08-112700-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>挿入後、MongoDB Atlas Database（<a href="https://www.mongodb.com/atlas/database">https://www.mongodb.com/atlas/database</a>）にアクセスしログインして、データベースの状態を確認すると、Cluster0というデータベース内にtest_1というコレクションが追加され、コレクション内にデータ（キーと値）が挿入されていることが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/creating-a-collection-and-inserting-keys-and-values-in-mongodbatlas-database-using-pymongo-in-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
