LoginSignup
1
2

More than 5 years have passed since last update.

mongoDBコマンド関係[メモ]

Posted at

概要

mongoDBの起動からのコマンド関連をまとめておきます。

起動

mongoDBを使うにあたり、データベース側のmongodを起動させます。

~
$ sudo mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb.log

--dbpathで、mongoDBのデータベースとなるpathを指定します。
--logpathで、mongoDBのlogの保存先を指定します。

Macにログインするときのpasswordを入力すると、mongodが起動します。

次に、別のターミナルを開き、以下のコマンドでクライエント側のmongoを起動させます。

~
$ mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
Server has startup warnings:
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten]
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten]
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-08T16:22:38.680+0900 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service to collect 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 created for you. Anyone you share the URL with will also be able to
view this page. 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()
---

>

コマンド関連

データベースの表示

~
> show dbs;
admin    0.000GB
config   0.000GB
local    0.000GB
mydb     0.000GB

データベースの選択(切り替え)

~
> use mydb
switched to db mydb

データベースの削除

~
> db.dropDatabase();

コレクションの表示

~
> show collections
users

コレクションの作成

~
> db.createCollection("users");
{ "ok" : 1 }

コレクションの削除

~
> db.users.drop();
true

コレクションにドキュメントを追加する

~
> db.users.insert( { name:'cat', score:39 } );
WriteResult({ "nInserted" : 1 })

表示(全件取得)

~
db.users.find();

ドキュメントの全件削除

~
db.users.remove({});
1
2
0

Register as a new user and use Qiita more conveniently

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