首页 > 数据库 >MongoDB 数据库部署和应用

MongoDB 数据库部署和应用

时间:2023-03-22 15:03:12浏览次数:38  
标签:部署 MongoDB 数据库 db local mongodb usr centos01 root

MongoDB 数据库部署和应用_配置文件

推荐步骤:

➢ 在 Centos01 上部署 MongoDB 服务器客户端登录验证

➢ 在 Centos01 的 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置 MongoDB 身份验证

➢ 在 Centos01 的 MongoDB 服务器配置身份验证管理和修改配置文件支持验证

➢ 在 Centos01 管理 MongoDB 管理数据,集合批量数据管理

实验步骤:

一、在 Centos01 上安装 mongoDB 数据库管理 mongoDB 服务

1、安装 MongoDB

1)创建管理 MongoDB 组和用户

[root@centos01 ~]# groupadd mongod

[root@centos01 ~]# useradd -M -s /sbin/nologin -g mongod mongod

2)解压指定 MongoDB 安装位置[root@centos01 ~]# mv /usr/src/mon

godb-linux-x86_64-rhel70-4.2.24/ 

/usr/local/mongodb

3)优化命令

[root@centos01 ~]# echo "PATH=$PATH:/usr/local/mongodb/bin/" >> /etc/profile

[root@centos01 ~]# source /etc/profile

2、配置 mongodb 配置文件日志文件初始化 mongoDB

1)创建 MongoDB 数据库的配置文件

[root@centos01 ~]# mkdir /usr/local/mongodb/conf

[root@centos01 ~]# mkdir /usr/local/mongodb/log

[root@centos01 ~]# mkdir /usr/local/mongodb/data

[root@centos01 ~]# chown -R mongod:mongod /usr/local/mongodb/

2)初始化 MongoDB 数据库启动服务

[root@centos01 ~]# mongod --dbpath=/usr/local/mongodb/data/ --

logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork

about to fork child process, waiting until server is ready for connections.

forked process: 1885

child process started successfully, parent exiting

3)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep mongod

tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1885/mongod

3、登录 mongoDB 测试 mongoDB

1)登录 MongoDB

[root@centos01 ~]# mongoTo permanently disable this reminder, run the following command: 

db.disableFreeMonitoring()

---

>

2)查看 MongoDB 数据库

> show databases;

admin 0.000GB

config 0.000GB

local 0.000GB

> show dbs

admin 0.000GB

config 0.000GB

local 0.000GB

3)切换数据库查看表

> use admin

switched to db admin

> show tables

system.version

> exit

bye二、生成 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置

MongoDB 身份验证

1、配置生成 MongoDB 配置文件

1)生成 MongoDB 配置文件

[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf

systemLog:

destination: file

path: "/usr/local/mongodb/log/mongodb.log"

logAppend: true

storage:

journal:

enabled: true

dbPath: "/usr/local/mongodb/data/"

processManagement:

fork: true

net:

port: 27017

bindIp: 192.168.100.10,127.0.0.1

2)修改配置文件所有者

[root@centos01 ~]# chown mongod:mongod /usr/local/mongodb/conf/mongo.conf

3)停止服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 1885

2、通过配置文件启动停止 mongoDB

1)通过配置文件启动 mongoDB

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 3663

child process started successfully, parent exiting

2)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep mongod

tcp 0 0 192.168.100.10:27017 0.0.0.0:* LISTEN 3663/mongod

tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3663/mongod

3)通过配置文件停止服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown

killing process with pid: 3663

3、登录 mongoDB 停止服务

1)启动 mongoDB 服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 3922

child process started successfully, parent exiting

2)登录 mongoDB 切换到 admin 数据库停止服务

[root@centos01 ~]# mongo

> use admin

switched to db admin

> db.shutdownServer()2023-03-16T00:23:10.633+0800 I NETWORK [js] DBClientConnection failed to receive

message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer

server should be down...

三、配置 mongoDB 身份验证管理和修改配置文件支持验证

1、MongDB 创建管理员和读取写入数据用户

1)创建管理员用户

[root@centos01 ~]# mongo

> use admin //切换到admin数据库

switched to db admin

> db.createUser( //db.createUser函数创建用户

... {

... user: "root", //用户命令root

... pwd: "pwd@123", //授权密码

... roles: [ { role: "root", db: "admin" } ]
//授权角色

... }

)

Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}]

}

> exit

bye

2)使用 root 登录创建应用程序访问用户授权读取和写入权限

[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin

> use admin

switched to db admin

> db.createUser(

... {

... user: "bob",

... pwd: "pwd@123",

... roles: [ { role: "readWrite", db: "bob" } ]

... }

... )

Successfully added user: {

"user" : "bob",

"roles" : [

{

"role" : "readWrite",

"db" : "bob"

}

]

​1)创建管理 MongoDB 组和用户

[root@centos01 ~]# groupadd mongod

[root@centos01 ~]# useradd -M -s /sbin/nologin -g mongod mongod

2)解压指定 MongoDB 安装位置

[root@centos01 ~]# mv /usr/src/mongodb-linux-x86_64-rhel70-4.2.24/ 

/usr/local/mongodb

3)优化命令

[root@centos01 ~]# echo "PATH=$PATH:/usr/local/mongodb/bin/" >> /etc/profile

[root@centos01 ~]# source /etc/profile

2、配置 mongodb 配置文件日志文件初始化 mongoDB

1)创建 MongoDB 数据库的配置文件

[root@centos01 ~]# mkdir /usr/local/mongodb/conf

[root@centos01 ~]# mkdir /usr/local/mongodb/log

[root@centos01 ~]# mkdir /usr/local/mongodb/data

[root@centos01 ~]# chown -R mongod:mongod /usr/local/mongodb/

2)初始化 MongoDB 数据库启动服务

[root@centos01 ~]# mongod --dbpath=/usr/local/mongodb/data/ --

logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork

about to fork child process, waiting until server is ready for connections.

forked process: 1885

child process started successfully, parent exiting

3)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep mongod

tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1885/mongod

3、登录 mongoDB 测试 mongoDB

1)登录 MongoDB

[root@centos01 ~]# mongoTo permanently disable this reminder, run the following command: 

db.disableFreeMonitoring()

---

>

2)查看 MongoDB 数据库

> show databases;

admin 0.000GB

config 0.000GB

local 0.000GB

> show dbs

admin 0.000GB

config 0.000GB

local 0.000GB

3)切换数据库查看表

> use admin

switched to db admin

> show tables

system.version

> exit

bye二、生成 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置

MongoDB 身份验证

1、配置生成 MongoDB 配置文件

1)生成 MongoDB 配置文件

[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf

systemLog:

destination: file

path: "/usr/local/mongodb/log/mongodb.log"

logAppend: true

storage:

journal:

enabled: true

dbPath: "/usr/local/mongodb/data/"

processManagement:

fork: true

net:

port: 27017

bindIp: 192.168.100.10,127.0.0.1

2)修改配置文件所有者

[root@centos01 ~]# chown mongod:mongod /usr/local/mongodb/conf/mongo.conf

3)停止服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown
killing process with pid: 1885

2、通过配置文件启动停止 mongoDB

1)通过配置文件启动 mongoDB

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 3663

child process started successfully, parent exiting

2)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep mongod

tcp 0 0 192.168.100.10:27017 0.0.0.0:* LISTEN 3663/mongod

tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3663/mongod

3)通过配置文件停止服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown

killing process with pid: 3663

3、登录 mongoDB 停止服务

1)启动 mongoDB 服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 3922

child process started successfully, parent exiting

2)登录 mongoDB 切换到 admin 数据库停止服务

[root@centos01 ~]# mongo

> use admin

switched to db admin

> db.shutdownServer()2023-03-16T00:23:10.633+0800 I NETWORK [js] DBClientConnection failed to receive

message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer

server should be down...

三、配置 mongoDB 身份验证管理和修改配置文件支持验证

1、MongDB 创建管理员和读取写入数据用户

1)创建管理员用户

[root@centos01 ~]# mongo

> use admin //切换到admin数据库

switched to db admin

> db.createUser( //db.createUser函数创建用户

... {

... user: "root", //用户命令root

... pwd: "pwd@123", //授权密码

... roles: [ { role: "root", db: "admin" } ]
//授权角色

... }

)

Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}]

}

> exit

bye

2)使用 root 登录创建应用程序访问用户授权读取和写入权限

[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin

> use admin

switched to db admin

> db.createUser(

... {

... user: "bob",

... pwd: "pwd@123",

... roles: [ { role: "readWrite", db: "bob" } ]

... }

... )

Successfully added user: {

"user" : "bob",

"roles" : [

{

"role" : "readWrite",

"db" : "bob"

}

]

}

exitbye

1)创建管理 MongoDB 组和用户

[root@centos01 ~]# groupadd mongod

[root@centos01 ~]# useradd -M -s /sbin/nologin -g mongod mongod

2)解压指定 MongoDB 安装位置

[root@centos01 ~]# mv /usr/src/mongodb-linux-x86_64-rhel70-4.2.24/ 

/usr/local/mongodb

3)优化命令

[root@centos01 ~]# echo "PATH=$PATH:/usr/local/mongodb/bin/" >> /etc/profile
[root@centos01 ~]# source /etc/profile

2、配置 mongodb 配置文件日志文件初始化 mongoDB

1)创建 MongoDB 数据库的配置文件

[root@centos01 ~]# mkdir /usr/local/mongodb/conf

[root@centos01 ~]# mkdir /usr/local/mongodb/log

[root@centos01 ~]# mkdir /usr/local/mongodb/data

[root@centos01 ~]# chown -R mongod:mongod /usr/local/mongodb/

2)初始化 MongoDB 数据库启动服务

[root@centos01 ~]# mongod --dbpath=/usr/local/mongodb/data/ --

logpath=/usr/local/mongodb/log/mongodb.log --port=27017 --logappend --fork

about to fork child process, waiting until server is ready for connections.

forked process: 1885

child process started successfully, parent exiting

3)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep mongod

tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1885/mongod

3、登录 mongoDB 测试 mongoDB

1)登录 MongoDB

[root@centos01 ~]# mongoTo permanently disable this reminder, run the following command: 

db.disableFreeMonitoring()

---

>

2)查看 MongoDB 数据库

> show databases;

admin 0.000GB

config 0.000GB

local 0.000GB

> show dbs

admin 0.000GB

config 0.000GB

local 0.000GB

3)切换数据库查看表

> use admin

switched to db admin

> show tables

system.version

> exit
bye

二、生成 MongoDB 配置文件通过控制文件控制 MongoDB 服务,配置

MongoDB 身份验证

1、配置生成 MongoDB 配置文件

1)生成 MongoDB 配置文件

[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf

systemLog:

destination: file

path: "/usr/local/mongodb/log/mongodb.log"

logAppend: true

storage:

journal:

enabled: true

dbPath: "/usr/local/mongodb/data/"

processManagement:

fork: true

net:

port: 27017

bindIp: 192.168.100.10,127.0.0.1

2)修改配置文件所有者

[root@centos01 ~]# chown mongod:mongod /usr/local/mongodb/conf/mongo.conf

3)停止服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown

killing process with pid: 1885

2、通过配置文件启动停止 mongoDB

1)通过配置文件启动 mongoDB

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 3663

child process started successfully, parent exiting

2)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep mongod

tcp 0 0 192.168.100.10:27017 0.0.0.0:* LISTEN 3663/mongod

tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3663/mongod

3)通过配置文件停止服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown

killing process with pid: 3663

3、登录 mongoDB 停止服务

1)启动 mongoDB 服务

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 3922

child process started successfully, parent exiting

2)登录 mongoDB 切换到 admin 数据库停止服务

[root@centos01 ~]# mongo

> use admin

switched to db admin

> db.shutdownServer()2023-03-16T00:23:10.633+0800 I NETWORK [js] DBClientConnection failed to receive

message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer

server should be down...

三、配置 mongoDB 身份验证管理和修改配置文件支持验证

1、MongDB 创建管理员和读取写入数据用户

1)创建管理员用户

[root@centos01 ~]# mongo

> use admin //切换到admin数据库

switched to db admin

> db.createUser( //db.createUser函数创建用户

... {

... user: "root", //用户命令root

... pwd: "pwd@123", //授权密码

... roles: [ { role: "root", db: "admin" } ]
//授权角色

... }

)

Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}]

}

> exit

bye

2)使用 root 登录创建应用程序访问用户授权读取和写入权限

[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin

> use admin

switched to db admin

> db.createUser(

... {

... user: "bob",

... pwd: "pwd@123",

... roles: [ { role: "readWrite", db: "bob" } ]

... }

... )

Successfully added user: {

"user" : "bob",

"roles" : [

{

"role" : "readWrite",

"db" : "bob"

}

]

}

> exix
bye

​3)使用普通用户登录 mongoDB

[root@centos01 ~]# mongo -ubob -ppwd@123 192.168.100.10/admin

2、修改 mongoDB 主配置文件支持验证

1)修改 mongoDB 主配置文件

[root@centos01 ~]# vim /usr/local/mongodb/conf/mongo.conf

systemLog:

destination: file

path: "/usr/local/mongodb/log/mongodb.log"

logAppend: true

storage:

journal:

enabled: true

dbPath: "/usr/local/mongodb/data/"

processManagement:

fork: true

net:

port: 27017

bindIp: 192.168.100.10,127.0.0.1

security:

authorization: enabled

2)停止服务启动加载身份验证

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.conf --shutdown

killing process with pid: 4337

[root@centos01 ~]# mongod -f /usr/local/mongodb/conf/mongo.confabout to fork child process, waiting until server is ready for connections.

forked process: 6113

child process started successfully, parent exiting

3、配置客户端登录进行身份验证

1)通过 IP 地址或者数据库验证登录

[root@centos01 ~]# mongo -uroot -ppwd@123 admin 

[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin

2)登录进行验证

[root@centos01 ~]# mongo

> use admin

switched to db admin

> db.auth('root','pwd@123')

1

> show dbs;

admin 0.000GB

config 0.000GB

local 0.000GB

3)查看创建授权的用户

> use admin //切换到admin数据库

switched to db admin

> db.system.users.find() //查看users表中创建的用户信息

{ "_id" : "admin.root", "userId" : UUID("bbbb8384-552d-43b7-bdbd-849c811a7fba"),

"user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" :

{ "iterationCount" : 10000, "salt" : "OWAQ5bhvBM5xsUgG57vrRg==", "storedKey" :

"VhzOEnNHc1EneG2JwkIb/hgVzqc=", "serverKey" : "N95vRRKEPtbpy0M0PtUH4WbXidc=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" :

"srIf+zffJydaFBlm7NoOtJCCb+VF4/28B3Vigw==", "storedKey" :

"ZQXhhxJ2NpaNuMw2CvfXic+3KcTEE150Mk/fJ55WDM4=", "serverKey" :

"kCjYi9BLpDyL+kDRoQzNsKgsOZtJvQjXT9dW+/cWN4k=" } }, "roles" : [ { "role" : "root",

"db" : "admin" } ] }

{ "_id" : "admin.bob", "userId" : UUID("23c4a9a8-8765-440e-bc1c-9bd0892c6661"),

"user" : "bob", "db" : "admin", "credentials" : { "SCRAM-SHA-1" :

{ "iterationCount" : 10000, "salt" : "GOIEIxWUxjvXqNYlYdaplQ==", "storedKey" :

"6HBrMz8gUVURJWsV3TKb3ITB4pk=", "serverKey" : "rh7mibsdVUfcoQtbaV2PImqPlUM=" },

"SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" :

"uTrQFcgpS/yxvc6yUmJ9U58Jwv51IalUOo223w==", "storedKey" :

"8q+7cXeEkL4ZyEMokSDzwsvay6vqjl9nxLy6zu3P27A=", "serverKey" :

"u24JQWS0TXn1Fmj0bnjrc8OUzzCdGICI3xN+EX7X00A=" } }, "roles" : [ { "role" :

"readWrite", "db" : "bob" } ] }

> exit

bye

4)查看用户信息

> db.system.users.find().pretty()

{

"_id" : "admin.root",

"userId" : UUID("bbbb8384-552d-43b7-bdbd-849c811a7fba"),

"user" : "root",

"db" : "admin",

"credentials" : {

"SCRAM-SHA-1" : {"iterationCount" : 10000,

"salt" : "OWAQ5bhvBM5xsUgG57vrRg==",

"storedKey" : "VhzOEnNHc1EneG2JwkIb/hgVzqc=",

"serverKey" : "N95vRRKEPtbpy0M0PtUH4WbXidc="

},

"SCRAM-SHA-256" : {

"iterationCount" : 15000,

"salt" : "srIf+zffJydaFBlm7NoOtJCCb+VF4/28B3Vigw==",

"storedKey" : "ZQXhhxJ2NpaNuMw2CvfXic+3KcTEE150Mk/fJ55WDM4=",

"serverKey" : "kCjYi9BLpDyL+kDRoQzNsKgsOZtJvQjXT9dW+/cWN4k="

}

},

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

{

"_id" : "admin.bob",

"userId" : UUID("23c4a9a8-8765-440e-bc1c-9bd0892c6661"),

"user" : "bob",

"db" : "admin","credentials" : {

"SCRAM-SHA-1" : {

"iterationCount" : 10000,

"salt" : "GOIEIxWUxjvXqNYlYdaplQ==",

"storedKey" : "6HBrMz8gUVURJWsV3TKb3ITB4pk=",

"serverKey" : "rh7mibsdVUfcoQtbaV2PImqPlUM="

},

"SCRAM-SHA-256" : {

"iterationCount" : 15000,

"salt" : "uTrQFcgpS/yxvc6yUmJ9U58Jwv51IalUOo223w==",

"storedKey" : "8q+7cXeEkL4ZyEMokSDzwsvay6vqjl9nxLy6zu3P27A=",

"serverKey" : "u24JQWS0TXn1Fmj0bnjrc8OUzzCdGICI3xN+EX7X00A="

}

},

"roles" : [

{

"role" : "readWrite",

"db" : "bob"

}

]

}

四、配置 mongoDB 基本管理

1、mongoDB 数据库基本管理

1)查看 mongoDB 数据库版本

[root@centos01 ~]# mongo -uroot -ppwd@123 192.168.100.10/admin

> db.version()

4.2.24

2)显示当前数据库

> db

admin

3)查看当前数据库状态

> db.stats()

{

"db" : "admin",

"collections" : 2,

"views" : 0,

"objects" : 4,

"avgObjSize" : 287.25,

"dataSize" : 1149,

"storageSize" : 73728,

"numExtents" : 0,

"indexes" : 3,

"indexSize" : 110592,

"scaleFactor" : 1,

"fsUsedSize" : 4763291648,

"fsTotalSize" : 81353875456,"ok" : 1

}

4)查看数据库连接请求

> db.getMongo()

connection to 192.168.100.10:27017

5)切换到指定数据库

> use benet

switched to db benet

6)benet 数据库创建表 student 表插,id 插入 1

> db.student.insert({id:1})

WriteResult({ "nInserted" : 1 })

7)查看创建数据库

> show dbs

admin 0.000GB

benet 0.000GB

config 0.000GB

local 0.000G

8)删除当前数据库

> db.dropDatabase()

{ "dropped" : "benet", "ok" : 1 }

2、集合的基本管理

1)创建 a 表插入数据

> db.a.insert({id:1})

WriteResult({ "nInserted" : 1 })

> db.b.insert({id:2})WriteResult({ "nInserted" : 1 })

2)查看表

> show tables;

a

b

3)查询表中数据

> db.a.find()

{ "_id" : ObjectId("641200d9810124bd98a26a78"), "id" : 1 }

4)创建集合名字 c 查看创建集合

> db.createCollection('c');

{ "ok" : 1 }

> show tables;

a

b

c

5)删除集合

> db.c.drop()

true

> show tables;

a

b

3、插入数据和批量插入数据

1)accp 库创建集合 t1 插入数据

> use accp

switched to db accp> db.t1.insert({id:110,name:"bob"})

WriteResult({ "nInserted" : 1 })

> db.t1.find()

{ "_id" : ObjectId("64120367810124bd98a26a7a"), "id" : 110, "name" : "bob" }

> db.t1.insert({姓名:"张三",年龄:18,学历:"大专"})

WriteResult({ "nInserted" : 1 })

> db.t1.find()

{ "_id" : ObjectId("64120367810124bd98a26a7a"), "id" : 110, "name" : "bob" }

{ "_id" : ObjectId("641203c3810124bd98a26a7b"), "姓名" : "张三", "年龄" : 18, "学历

" : "大专" }

2)插入多个连续连数据

> db.t1.insertMany([

... {name:"bob"},

... {name:"tom"},

... {name:"alice"}

... ])

{

"acknowledged" : true,

"insertedIds" : [

ObjectId("641205bd810124bd98a26a7c"),

ObjectId("641205bd810124bd98a26a7d"),

ObjectId("641205bd810124bd98a26a7e")

]

}

> db.t1.find(){ "_id" : ObjectId("64120367810124bd98a26a7a"), "id" : 110, "name" : "bob" }

{ "_id" : ObjectId("641203c3810124bd98a26a7b"), "姓名" : "张三", "年龄" : 18, "学历

" : "大专" }

{ "_id" : ObjectId("641205bd810124bd98a26a7c"), "name" : "bob" }

{ "_id" : ObjectId("641205bd810124bd98a26a7d"), "name" : "tom" }

{ "_id" : ObjectId("641205bd810124bd98a26a7e"), "name" : "alice" }

3)批量化插入数据

> for(i=0;i<100;i++){ db.t1.insert({"编号":i,"名字":"test","年龄":18,"date":new 

Date()});}

WriteResult({ "nInserted" : 1 })

4)查询编号为 50 的记录

> db.t1.find({编号:50})

{ "_id" : ObjectId("6412071c810124bd98a26ab1"), "编号" : 50, "名字" : "test", "年龄

" : 18, "date" : ISODate("2023-03-15T17:57:48.513Z") }

5)清空 t1 表中数据

> db.t1.remove({});


标签:部署,MongoDB,数据库,db,local,mongodb,usr,centos01,root
From: https://blog.51cto.com/u_15947624/6142636

相关文章