6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MongoDB Shell の使い方

Last updated at Posted at 2021-05-12

参考ページ
MongoDB Shell (mongosh)

MongoDB のサーバーが動いていることを確認

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor prese>
     Active: active (running) since Wed 2021-05-12 09:24:40 JST; 6min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 30875 (mongod)
     Memory: 200.2M
     CGroup: /system.slice/mongod.service
             └─30875 /usr/bin/mongod --config /etc/mongod.conf

Shell を起動

$ mongosh
Current Mongosh Log ID:	63fd5cc53f9b4d91ff752684
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.7.1
Using MongoDB:		6.0.4
Using Mongosh:		1.7.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-02-28T09:44:22.302+09:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2023-02-28T09:44:22.500+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2023-02-28T09:44:22.500+09:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-02-28T09:44:22.500+09:00: vm.max_map_count is too low
------

バージョンの確認

test> db.version()
6.0.4

データベースの表示

test> show dbs
admin    152 kB
city    73.7 kB
config  36.9 kB
local   73.7 kB
testDB  73.7 kB

city というデータベースに saitama という collection が作成されている時の操作

test> use city
switched to db city
city> show collections
saitama
city> db.saitama.find ()
[
  {
    _id: ObjectId("6223fbd2e854c44f3da6a38b"),
    key: 't1161',
    name: 'さいたま',
    population: 78941,
    date_mod: '2001-6-8'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a38c"),
    key: 't1162',
    name: '所沢',
    population: 41987,
    date_mod: '2001-10-9'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a38d"),
    key: 't1163',
    name: '越谷',
    population: 93712,
    date_mod: '2001-5-21'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a38e"),
    key: 't1164',
    name: '久喜',
    population: 54291,
    date_mod: '2001-7-25'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a38f"),
    key: 't1165',
    name: '熊谷',
    population: 87523,
    date_mod: '2001-3-12'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a390"),
    key: 't1166',
    name: '秩父',
    population: 43927,
    date_mod: '2001-4-19'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a391"),
    key: 't1167',
    name: '川越',
    population: 68734,
    date_mod: '2001-7-30'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a392"),
    key: 't1168',
    name: '和光',
    population: 24169,
    date_mod: '2001-8-22'
  },
  {
    _id: ObjectId("6223fbd2e854c44f3da6a393"),
    key: 't1169',
    name: '新座',
    population: 85642,
    date_mod: '2001-4-12'
  }
]
city> 

終了

city> exit

ドキュメントのカウント

city> db.saitama.countDocuments({})
9

表示数の制限

city> db.saitama.find().limit(2)
[
  {
    _id: ObjectId("6260fdbd1d6dd9a1ae862d85"),
    key: 't1161',
    name: 'さいたま',
    population: 82913,
    date_mod: '2003-8-20'
  },
  {
    _id: ObjectId("6260fdbd1d6dd9a1ae862d86"),
    key: 't1162',
    name: '所沢',
    population: 67415,
    date_mod: '2003-5-10'
  }
]

ドキュメントをスキップして表示

city> db.saitama.find().skip(3).limit(2)
[
  {
    _id: ObjectId("6260fdbd1d6dd9a1ae862d88"),
    key: 't1164',
    name: '久喜',
    population: 53672,
    date_mod: '2003-9-9'
  },
  {
    _id: ObjectId("6260fdbd1d6dd9a1ae862d89"),
    key: 't1165',
    name: '熊谷',
    population: 42391,
    date_mod: '2003-8-4'
  }
]

検索

city> db.saitama.find({name: '熊谷'})
[
  {
    _id: ObjectId("626134c70afe242ac92a8754"),
    key: 't1165',
    name: '熊谷',
    population: 42391,
    date_mod: '2003-8-4'
  }
]

コレクションの削除

city> db.saitama.drop()
true

コレクションの作成

city> show collections
saitama
tochgi
city> db.createCollection("ibaraki")
{ ok: 1 }
city> show collections
ibaraki
saitama
tochgi
city>

コレクションの作成から、データの挿入まで

city> db.saitama.drop()
true
city> db.createCollection("saitama")
{ ok: 1 }
city> tx = {"key":"t1161","name":"さいたま","population":78941,"date_mod":"2001-6-8"}
{ key: 't1161', name: 'さいたま', population: 78941, date_mod: '2001-6-8' }
city> db.saitama.insertOne (tx)
{
  acknowledged: true,
  insertedId: ObjectId('667a8c132338f5d7aaa26a15')
}
city> db.saitama.find()
[
  {
    _id: ObjectId('667a8c132338f5d7aaa26a15'),
    key: 't1161',
    name: 'さいたま',
    population: 78941,
    date_mod: '2001-6-8'
  }
]
city>
6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?