MongoDB(1) - 準備編

今回からMongoDBに関する記事を書いていきます。

コンソールからの操作とその操作をPythonで行う場合の2種類でMongoDBの動作確認を行います。

(最近Python関連の記事を全く書いていないことに気づきました・・・😨😨)

MongoDBとは

MongoDBは、NoSQLの1つです。

NoSQLの中でもMongoDBはシェアがトップという調査結果があります。

MongoDBが選ばれる理由としては下記が挙げられます。

  • スキーマレスにより保守性が高い
  • 高パフォーマンス
  • RDBと似た操作により学習効率が高い
  • オープンソースのため無料
  • 簡単な設定でスケールアウト(負荷分散)が可能

MongoDBのインストール

下記サイトからMongoDBのインストーラをダウンロードし、MongoDBをインストールします。

各環境に合わせてインストールを行ってください。

MongoDB Community Server - https://www.mongodb.com/try/download/community

(Community Serverが無料版になります。)

コンソールの準備(Mongoシェル)

インストール先にbinフォルダ(ディレクトリ)があり、そこにパスを通しておくとコンソールからMongoシェルを実行することができます。

コンソールからmongoと入力すると、Mongoシェルが起動します。

[コンソール]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>mongo
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("958c42c1-e0fe-45ec-b53e-d1a0db49c950") }
MongoDB server version: 5.0.2
================
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.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
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:
2021-08-14T09:24:46.540+09: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()
---
>

上記のように表示されたら、成功です。

このMongoシェルから、MongoDBを操作することができます。

Python用の準備

PythonからMongoDBを操作するためにはpymongoライブラリを使用します。

pipコマンドでインストールします。

[コンソール]

1
2
3
4
5
6
>pip install pymongo
Collecting pymongo
Downloading pymongo-3.12.0-cp38-cp38-win_amd64.whl (397 kB)
|████████████████████████████████| 397 kB 6.8 MB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.12.0

以上で、コンソールとPythonからMongoDBを利用する準備ができました。


次回から、MongoDBをいろいろと操作していきます。