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" : [ ]
})

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值