02:MongoDB操作

Posted 不做大哥好多年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02:MongoDB操作相关的知识,希望对你有一定的参考价值。

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操作的主要内容,如果未能解决你的问题,请参考以下文章

我可以在片段中构建操作栏吗?

在 Spring MongoDB 的 ReplaceRoot 管道阶段使用 $mergeObjects

python3操作MongoDB的crud以及聚合案例,代码可直接运行(python经典编程案例)

02 MongoDB数据类型重要概念以及shell常用指令

02 MongoDB数据类型重要概念以及shell常用指令

02:MongoDB操作