1、mongoDB简介:mongoDB 为非关系数据库,集合(collection)关系数据库中的表,中存储的为json结构的文档,集合中的每一条记录都可以结构不同,

但必须都有_id字段(mongoDB默认的Object类型的标识)。

2、mongoDB安装:教程链接:Windows下:http://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html

Linux下:http://jingyan.baidu.com/article/fd8044faf4f3a95030137a79.html

3、mongoDB初级查询语句:

mongoDB语句 对应的SQL语句
新建数据库:use mydb;//新建一个数据库mydb create database mydb
查看数据库列表:show dbs //mongoDB自带测试数据库  

新建集合:db.student.insert({'name':'lily',age:NumberInt('18','sex':'女')})

插入新的数据,mongoDB自动创建集合,无数据的集合不会显示在列表中

create table student(
            id varchar(30),
            name varchar(30),
            age int,
            sex varchar(10),
            primary key (id)
        )
查询全部:db.collection.find(); select * from collection;

条件查询:db.collection.find({age:18})

db.collection.find({'sex':'男'})

db.collection.find({age:{$gt:18}})

db.collection.find({age:{$gte:18}})

db.collection.find({age:{$lt:18}})

db.collection.find({age:{$lte:18}})

select * from collection where age=18;

select * from collection where sex='男';

select * from collection where age>18;

select * from collection where age>=18;

select * from collection where age<18;

select * from collection where age<=18;

 查询数量:db.collection.find({age:18}).count();  select count(*) from collection where age=18;
 分页查询:db.collection.find({age:18}).limit(10).skip(5);  select * from collection where age=18 limit 5,10;
 查询指定字段:db.collection.find({age:18},{name:1})  select name from collection where age=18;
插入数据:db.collection.insert({'name':'lucy',age:28})  insert into collection(name,age) values('lucy',28)
删除数据:db.collection.drop({'name':'lucy'}) delete from collection where name='lucy'
更新数据:db.collection.update({'name':'lily'},{$set:{age:19}}) update  collection set age=19 where name='lily';
模糊条件查询:db.collection.find({'name':/li/}) select * from collection where name like %li%;
删除集合:db.collection.drop();  drop table collection;

删除数据库 ues mydb

db.dropDatebase();

 

4、mongoDB 高级查询:

A、聚合:mongoDB 中的aggregate()方法,相当于mysql中的group by。基本语法是:db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION);

eg:db.collection.aggregete([{$match:{age:18}},{$group:{_id:'sex',num:{$sum:1}}}])

对应SQL:select sex count(*) from collection where age=18 group by sex;

B、排序:mongoDB 中的sort()方法,相当于mysql中的order by 。

eg:db.collection.find({'sex':'男'}).sort({age:1});

对应SQL:select * from collection where sex='男' reder by age ASC

db.collection.find({'sex':'男'}).sort({age:-1});

对应SQL:select * from collection where sex='男' reder by age DESC

C.去重:mongoDB中的distinct()方法去重。

eg:db.collection.distinct("name")

对应SQL:select distinct(name) from collection;

D、执行分析:mongoDB中用explain()分析执行过程。

eg:db.collection.find({age:18}).explain();

对应的SQL:explain select * from collection where age=18;

附:explain参数详解:https://my.oschina.net/foreverhui/blog/639240?p={{totalPage}}

5、mongoDB 特有查询:

a、$all匹配所有值

这个操作和SQL的in类似,但是in只需要满足其中的一个值即可,而$all则需要满足所有值。

eg:db.collection.find({age:{$all:[20,21]}})

可以查询出age[20,21,22]但查不出age[20,22,23]即必须同时有20和21。

b、$in查询包含的值

这个操作相当于SQL中的in。

eg:db.collection.find({age:{$in:[20,21]}})

c、$exists判断字段是否存在

eg:查询存在age字段的记录

db.collection.find({age:{$exists:true}})

查询不存在age字段的记录

db.collection.find({age:{$exists:false}})

d、null值的处理

mongoDB 中的null值处理不仅会返回某一字段为null的记录,还会返回不存在该字段的记录

eg:db.collection.find({age:null})

查询结果既有age字段为null的记录还会有不存在age字段的记录。如果需要只查出age字段为null的记录,需要配合$exists

eg:db.collection.find({age:{'$in':[null],'$exists':true}})

查询结果为存在age字段且该字段的值为null

e、$mod取模运算

eg:db.collection.find({age:{$mod:[5,3]}})

查询age取模5为3的记录。

f、$ne不等于操作

eg:db.collection.find({age:{$ne:18}})

g、$nin不包含操作

eg:db.collection.find({age:{$nin:[18,19,20]}})

查询age不等于 18、19、20的记录。

h、JavaScript查询和$where查询

eg:查询年龄大于18的记录有以下四种方式

条件操作符:db.collection.find({age:{$gt:18}})

$where 方式:db.collection.find({$where:'this.age>18'})

内部对象查询:db.collection.find("this.age>18")

JavaScript查询:func = function(){return this.age>18}

db.collection.find(func)

j、存储过程

创建存储过程:db.system.js.save({_id:'addNum',value:function(x,y){return x+y}})

查看所有存储过程:db.system.js.find()

调用存储过程:db.eval("addNum(2,3)")

同时可以直接用db.eval()创建并调用存储过程

db.eval(function (){return 2+3;})

存储过程可以处理数据库内部的操作

db.system.js.save({_id:"getCount",value:function(){return db.collection.find({age:{$gt:18}}).count()}})

将collection中age大于18的记录的数量保存在getCount这个存储过程中,调用时db.eval("getCount()")就可以直接得到那个数量

mongoDB数据库的更多相关文章

  1. Mongodb数据库学习系列————(一)Mongodb数据库主从复制的搭建

    Mongodb数据库主从复制的搭建 Writeby:lipeng                                    date:2014-10-22 最近项目上用到了位置查询,在网上 ...

  2. 基于C#的MongoDB数据库开发应用(4)--Redis的安装及使用

    在前面介绍了三篇关于MongoDB数据库的开发使用文章,严格来讲这个不能归类于MongoDB数据库开发,不过Redis又有着和MongoDB数据库非常密切的关系,它们两者很接近,Redis主要是内存中 ...

  3. FineReport如何连接和使用MongoDB数据库

    随着NoSQL数据库越来越流行,MongoDB数据库作为NoSQL数据库中的领头羊,使用也越来越广泛.为此,FineReport V8.0版本提供了数据连接和数据集接口,可以通过开发一款可以连接和使用 ...

  4. python操作mongodb数据库

    一.MongoDB 数据库操作 连接数据库 import pymongo conn = pymongo.Connection() # 连接本机数据库 conn = pymongo.Connection ...

  5. NoSql 中Mongodb数据库的使用

    1.NoSql数据库简介 2.MongoDB数据库的简介 3.MongoDB下Windows下的安装

  6. 线上mongodb数据库mLab使用总结

    最近在CNode社区看到有人分享了免费的线上mongodb数据库(容量500M),今天去注册了一下,成功的将线下数据库替换掉了,现在就说一下它的使用和配置需要注意的地方: mLab是一款免费的在线mo ...

  7. mongoDB数据库和Spring MVC的整合

    之前一直用到的项目是Spring MVC+maven+mysql的,最近有些数据需要用到mongoDB数据库,现在做一些总结. 第一步:加载jar.maven配置 <!-- mongodb开始 ...

  8. 【转载】CentOS6.5_X64下安装配置MongoDB数据库

    [转载]CentOS6.5_X64下安装配置MongoDB数据库 2014-05-16 10:07:09|  分类: 默认分类|举报|字号 订阅      下载LOFTER客户端 本文转载自zhm&l ...

  9. 基于C#的MongoDB数据库开发应用(3)--MongoDB数据库的C#开发之异步接口

    在前面的系列博客中,我曾经介绍过,MongoDB数据库的C#驱动已经全面支持异步的处理接口,并且接口的定义几乎是重写了.本篇主要介绍MongoDB数据库的C#驱动的最新接口使用,介绍基于新接口如何实现 ...

  10. 基于C#的MongoDB数据库开发应用(2)--MongoDB数据库的C#开发

    在上篇博客<基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用>里面,我总结了MongoDB数据库的一些基础信息,并在最后面部分简单介绍了数据库C#驱动的 ...

随机推荐

  1. ORACLE 数据库 MOD 函数用法

    1.求2和1的余数. Select mod(2,1) from dual: 2能被1整除所以余数为0. 2.MOD(x,y)返回X除以Y的余数.如果Y是0,则返回X的值. Select mod(2,0 ...

  2. AngularJS + Java---前台网页与后台数据库传递数据 基本结构

    第一个关于这两种语言的项目,以下只是我自己的理解,欢迎指教:) 基本对应关系 1. controller .jsp(.html)  ng-controller="controllerTest ...

  3. 一些好的python IDE

    pyscipter 是一个不错的选择,快速灵巧.功能丰富.它的安装包只有五六兆,功能却一个都不少.语法高亮功能也很强,运算符.数字.hex都能按照你的需要改变颜色.还有非常灵敏的code comple ...

  4. C++ 资源大全

    http://www.uml.org.cn/c++/201411145.asp http://ezlippi.com/blog/2014/12/c-open-project.html <C++ ...

  5. Window 消息大全

    消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序. 消息本身是作为一个记录传递给应用程序 ...

  6. IOS开发--自定义segment控件,方便自定义样式

    系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需 这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控 ...

  7. java内存的那些事

    在Java中,内存的管理分为以下几个部分: Heap:堆区域,存放对象实例,凡是New出来的东西都存放在此. Stack:栈区域,存放基本数据类型.常量.局部变量.对象的引用地址 Data Segme ...

  8. (AIDE)Android Eclipse JNI 调用 .so文件加载问题

    背景:对于Android工程 Eclipse里编译好的.so文件放到 libs\armeabi下以后, 这样.so文件就可以打包到apk文件里,在apk装到手机上以后 在libs\armeabi下的. ...

  9. 【STM32F4】读取芯片ID和芯片Flash Size

    首先声明,手册上给出的FlashSize地址是错误的,正确的应该是0x1FFF7A20,取高16位.确切说应该是(0x1FFF7A23,0x1FFF7A22两个字节), 芯片的这96位ID是产品唯一身 ...

  10. C++ CTime COleTime的一些操作技巧

    strCString="2003-10-27 6:24:37"; //CString--->COleDateTime COleVariant vtime(strCString ...