如何使用MongoDB Shell

参考页面
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
bannerAds