mongodb使用一

MongoDB shell version v3.4.24-1-g69fa4377f7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.24-1-g69fa4377f7
Server has startup warnings:
2020-03-29T14:08:02.836+0800 I CONTROL  [initandlisten]
2020-03-29T14:08:02.836+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-03-29T14:08:02.836+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-03-29T14:08:02.836+0800 I CONTROL  [initandlisten]
#转到数据库admin  切换数据库用use
> use admin  
switched to db admin
> show databases
admin  0.000GB
local  0.000GB
#创建一个新的用户   root权限是超级管理员
> db.createUser({user:'A',pwd:'123456',roles:['root']})   
Successfully added user: { "user" : "A", "roles" : [ "root" ] }

#登录
> db.auth('A','123456')
1
#Ctrl+L是清除以上内容   Ctrl+C是退出客户端
#创建新的数据库three:必须有了数据 才开始建库
#建库建表都是自动创建的,下面是直接往集合stu中插入数据,就表明建立了一个集合,有了数据库就建好了
> use three
switched to db three
> db.stu.insert({a:1})
WriteResult({ "nInserted" : 1 })
> show dbs
admin  0.000GB
local  0.000GB
three  0.000GB
#删除数据库:
> use three
switched to db three
> db.dropDatabase()
{ "dropped" : "three", "ok" : 1 }
#只有A root权限才能查看所有的用户
注意:千万别先删了root用户,否则就

 

#删除用户:db.dropUser('B')
#查看所有数据库:show dbs
 
#对集合的操作:
#查看所有集合:show collections
#创建集合:
> db.createCollection('abc')
{ "ok" : 1 }
#删除集合
> db.abc.drop()
true
#查看集合中的所有数据
> db.stu.find()
{ "_id" : ObjectId("5e819e9f75d26b8add679fe8"), "a" : 1 }
 
 

 

posted @ 2020-03-30 21:35  wind_y  阅读(302)  评论(2编辑  收藏  举报