码迷,mamicode.com
首页 > 数据库 > 详细

02:MongoDB操作

时间:2019-02-10 10:50:29      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:rtm   insert   class   tno   数据   EDA   comm   1.2   manage   

1.1 MongoDB 增加数据

  1、说明

      1. 插入文档: insert 如果插入数据的时候,collection还不存在,自动创建集合
      2. insertOne: 插入一条数据
      3. insertMany: 接收数组,插入多条文档

#1、插入单条数据
db.student1.insertOne({_id:"stu001","name":"Tom","age":25,grade:{"chinese":80,"math":90,"english":88}})

#2、插入多条数据
db.student1.insertMany([
{_id:"stu002","name":"Mary","age":23,grade:{"chinese":80,"math":90}},
{_id:"stu003","name":"Mike","age":23,grade:{"chinese":81,"math":90,"english":88}}
]);

1.2 MongoDB查询操作

  1、创建测试数据

技术图片
db.emp.insert(
[
{_id:7369,ename:SMITH ,job:CLERK    ,mgr:7902,hiredate:17-12-80,sal:800,comm:0,deptno:20},
{_id:7499,ename:ALLEN ,job:SALESMAN ,mgr:7698,hiredate:20-02-81,sal:1600,comm:300 ,deptno:30},
{_id:7521,ename:WARD  ,job:SALESMAN ,mgr:7698,hiredate:22-02-81,sal:1250,comm:500 ,deptno:30},
{_id:7566,ename:JONES ,job:MANAGER  ,mgr:7839,hiredate:02-04-81,sal:2975,comm:0,deptno:20},
{_id:7654,ename:MARTIN,job:SALESMAN ,mgr:7698,hiredate:28-09-81,sal:1250,comm:1400,deptno:30},
{_id:7698,ename:BLAKE ,job:MANAGER  ,mgr:7839,hiredate:01-05-81,sal:2850,comm:0,deptno:30},
{_id:7782,ename:CLARK ,job:MANAGER  ,mgr:7839,hiredate:09-06-81,sal:2450,comm:0,deptno:10},
{_id:7788,ename:SCOTT ,job:ANALYST  ,mgr:7566,hiredate:19-04-87,sal:3000,comm:0,deptno:20},
{_id:7839,ename:KING  ,job:PRESIDENT,mgr:0,hiredate:17-11-81,sal:5000,comm:0,deptno:10},
{_id:7844,ename:TURNER,job:SALESMAN ,mgr:7698,hiredate:08-09-81,sal:1500,comm:0,deptno:30},
{_id:7876,ename:ADAMS ,job:CLERK    ,mgr:7788,hiredate:23-05-87,sal:1100,comm:0,deptno:20},
{_id:7900,ename:JAMES ,job:CLERK    ,mgr:7698,hiredate:03-12-81,sal:950,comm:0,deptno:30},
{_id:7902,ename:FORD  ,job:ANALYST  ,mgr:7566,hiredate:03-12-81,sal:3000,comm:0,deptno:20},
{_id:7934,ename:MILLER,job:CLERK    ,mgr:7782,hiredate:23-01-82,sal:1300,comm:0,deptno:10}
]
);
创建emp表并插入14条数据

  2、基本查询

#1、查询所有的员工信息
db.emp.find()

#2、查询职位值经理的员工
db.emp.find({"job":"MANAGER"})

#3、操作符:$in和$or
#查询职位是MANAGER或者是CLERK员工信息
db.emp.find({"job":{$in:["MANAGER","CLERK"]}})
db.emp.find({$or:[{"job":"MANAGER"},{"job":"CLERK"}]})

#4、查询10号部门工资大于2000的员工
db.emp.find({"sal":{$gt:2000},"deptno":10})

  3、查询数组的文档

 

 

 

 

 

 

 

111111111111111111111111

02:MongoDB操作

标签:rtm   insert   class   tno   数据   EDA   comm   1.2   manage   

原文地址:https://www.cnblogs.com/xiaonq/p/10358669.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!