MongoDB命令相关[备忘录]

总结

我将把MongoDB的启动和相关命令整理在一起。

打开

在使用MongoDB时,需要启动数据库端的mongod。

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

— 使用–dbpath参数来指定MongoDB数据库的路径。
— 使用–logpath参数来指定MongoDB日志的保存路径。

输入密码时,将启动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({});