mongoDB学习

安装略

启动  用mongo shell 启动MongoDB进程

[root@www ~]# mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("450d079a-31d4-4673-9d68-8b2cd73edce3") }
MongoDB server version: 4.0.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2020-08-16T19:47:38.582+0800 I CONTROL  [initandlisten] 
2020-08-16T19:47:38.582+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-08-16T19:47:38.582+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-16T19:47:38.583+0800 I CONTROL  [initandlisten] 
2020-08-16T19:47:38.583+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-08-16T19:47:38.583+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-16T19:47:38.583+0800 I CONTROL  [initandlisten] 

ex:  如果使用docker 启动mongoDB的话 可以这样 

1,先要下载docker-mongoDB镜像

2 ,运行命令

//注解  运行       容器名      交互方式运行 挂载                        后台运行 镜像名
docker run --name myMongoDB4 -it        -v /www/mongo/data:data/db -d      mondb4

查看数据库

show databases
admin   0.000GB
config  0.000GB
local   0.000GB

创建数据库,这里如果使用这个myTest数据库的话,将会被释放 类似linux 的  vim 来创建文件,如何不写内容,退出后该文件会被释放掉

use myTest
switched to db myTest

查看该库的的集合(当前为空)  也就是mysql里的表

 show collections
> 

创建一个为person的集合  

db.person.insertOne(
... {
... _id:"id1",
... name:"nico",
... balance:100
... }
... )
//创建成功后返回一个json
//acknowledged 默认安全锁级别被启用
//insertedId 插入的ID
{ "acknowledged" : true, "insertedId" : "id1" }

再次查看该库下的集合,发现有一个peron集合

 show collections
person

使用insert【该命令可以写入一个也可以写入多条数据】命令写入集合

db.person.insert(
... {
... name :"luxi",
... balance:99
... }
... )
WriteResult({ "nInserted" : 1 })

如果insert遇到错误会返回什么样子的结果呢

db.person.insert(
... [
... {
... _id :"id1",
... name : "dno",
... balance:199
... },
... {
... name : "wusong",
... balance : 200
... }
... ])
BulkWriteResult({
	"writeErrors" : [
		{
			"index" : 0,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error collection: myTest.person index: _id_ dup key: { : \"id1\" }",
			"op" : {
				"_id" : "id1",
				"name" : "dno",
				"balance" : 199
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 0,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

 

 

 

 

 

你可能感兴趣的:(MongoDB)