MongoDB基本语句

  

<强>简介:
MongoDB与MySQL基本语句还是有很大区别的,今天再介绍一下MongoDB的一些常用的基本语句:

  

//进入创建数据库
使用学校;如果创建集合则自动创建数据库,如果没有创建集合则数据库没有创建

  

//创建集合
db.createCollection(类)

  

//查看库show dbs
,

  

//查看集合
显示表;

  

//插入数据
db.class.insert ({“id": 1、“name":“zhangsan"})
db.class.insert ({“sex":真正的“hobby": [“sport",“book",“run"]})
//查看数据
db.class.find ()

  

//多条插入
(var=2; i<=100;我+ +)db.class.insert ({“id":我,“name":“jack" + i});

  

//查看某一条数据
db.class.findOne ({“id": 2})

  

//查找指定记录并赋予别名一个
=db.class.findOne ({“id": 2})

  

//查看类型

  
  

typeof (a.id)

typeof (a.name)


字符串      

//修改数据:
db.info.update ({“id": 10},{$设置:{“name":“tom10"}})格式:条件在前,修改在后
db.info.findOne ({“id": 10})
{
“_id":ObjectId (“5 b972d38fb89e57a63998a84"),
“id":10,
“name":“tom10"

  

//聚合函数统计记录
db.info.count ()
101

  

//删除集合
db.class.drop ()

  

//删除数据库
db.dropDatabase ()

  

//删除数据
db.class.remove ({“id": 100});

  

//更改
db.class.update ({“id": 10},{$设置:{“name":“tom"}});
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -导入导出- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//插入100条数据
(var i=1; i<=100;我+ +)db.class.insert ({“id":我,“name":“jack" + i})
db.class.count ()
显示100

  

//数据导出
mongoexport - d - c类- o/opt/学校。杰森
//条件导出q’{“id": {“eq"美元:10}}“
mongoexport -学校- c类- q’{“id": {“eq"美元:10}}' - o/opt/school10.jason

  

//数据导入
mongoimport - d - c、舞台——文件/opt/学校。杰森
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -备份与恢复- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
创建好库,插入好数据后
[root@localhost选择]# mongodump - o - d学校/opt/
2018 - 09 - 12 t10:59:57.635 + 0800 school.info写入
2018 - 09 - 12 t10:59:57.639 + 0800进行倾销school.info文档(100)
[root@localhost选择]# cd/opt/
[root@localhost选择]# ls
rh学校
[root@localhost学校]# mongorestore - d myschool - dir=/opt/学校

  

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -克隆集合- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mongo端口27018

  
  

db.runCommand ({“cloneCollection":“kgc, info",“from":“192.168.120.128:27017"})
show dbs;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -创建管理用户- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
使用admin//进入用户
db.createUser ({“user":“root",“pwd":“123”,“roles": [“root"]})//设置用户密码
db.auth (“root",“123“)//验证用户密码

     

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -进程管理- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  
  

db.currentOp()//查看进程号
显示
“opid": 337,
db.killOp(337)//结束进程

  

MongoDB基本语句