MongoDB的基础查询和索引操作方法总结

所属分类: 数据库 / MongoDB 阅读数: 64
收藏 0 赞 0 分享

查询操作

1、查询所有记录

db.userInfo.find();

相当于:

select* from userInfo;

2、查询去掉后的当前聚集集合中的某列的重复数据

db.userInfo.distinct("name");

会过滤掉name中的相同数据

相当于:

select disttince name from userInfo;

3、查询age = 22的记录

db.userInfo.find({"age": 22});

相当于:

select * from userInfo where age = 22;

4、查询age > 22的记录

db.userInfo.find({age: {$gt: 22}});

相当于:

select * from userInfo where age >22;

5、查询age < 22的记录

db.userInfo.find({age: {$lt: 22}});

相当于:

select * from userInfo where age <22;

6、查询age >= 25的记录

db.userInfo.find({age: {$gte: 25}});

相当于:

select * from userInfo where age >= 25;

7、查询age <= 25的记录

db.userInfo.find({age: {$lte: 25}});

相当于:

select * from userInfo where age <= 25;

8、查询age >= 23 并且 age <= 26

db.userInfo.find({age: {$gte: 23, $lte: 26}});

相当于:

select * from userInfo where age >=23 and age <= 26;

9、查询name中包含 mongo的数据

db.userInfo.find({name: /mongo/});

相当于:

select * from userInfo where name like ‘%mongo%';

10、查询name中以mongo开头的

db.userInfo.find({name: /^mongo/});

相当于:

select * from userInfo where name like ‘mongo%';

11、查询指定列name、age数据

db.userInfo.find({}, {name: 1, age: 1});

相当于:

select name, age from userInfo;

当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。

12、查询指定列name、age数据, age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

相当于:

select name, age from userInfo where age >25;

13、按照年龄排序

升序:

db.userInfo.find().sort({age: 1});

降序:

db.userInfo.find().sort({age: -1});

14、查询前5条数据

db.userInfo.find().limit(5);

相当于:

select * from (select * from userInfo) where rownum < 6;//oracle

select * from userInfo limit 5;//mysql

15、查询10条以后的数据

db.userInfo.find().skip(10);

相当于:

select * from userInfo where id not in (select id from (select * from userInfo) where  and rownum < 11);

16、查询在5-10之间的数据
db.userInfo.find().limit(10).skip(5);

可用于分页,limit是pageSize,skip是第几页*pageSize

17、or与 查询

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

相当于:

select * from userInfo where age = 22 or age = 25;

18、查询第一条数据

db.userInfo.findOne();

db.userInfo.find().limit(1);

相当于:

select * from (select * from userInfo) where and rownum < 2

19、查询某个结果集的记录条数

db.userInfo.find({age: {$gte: 25}}).count();

相当于:select count(*) from userInfo where age >= 20;


索引

1、创建索引

db.userInfo.ensureIndex({username: 1});

在MongoDB中,我们同样可以创建复合索引,如下:

db.userInfo.ensureIndex({username: 1, age: -1});

该索引被创建后,基于username和age的查询将会用到该索引,或者是基于username的查询也会用到该索引,但是只是基于age的查询将不会用到该复合索引。因此可以说,如果想用到复合索引,必须在查询条件中包含复合索引中的前N个索引列。然而如果查询条件中的键值顺序和复合索引中的创建顺序不一致的话,MongoDB可以智能的帮助我们调整该顺序,以便使复合索引可以为查询所用。如:

db.test.find({"age": 30, "username": "stephen"})

对于上面示例中的查询条件,MongoDB在检索之前将会动态的调整查询条件文档的顺序,以使该查询可以用到刚刚创建的复合索引。

2、创建唯一索引

在缺省情况下创建的索引均不是唯一索引。下面的示例将创建唯一索引,如:

db.test.ensureIndex({"userid":1},{"unique":true})

如果再次插入userid重复的文档时,MongoDB将报错,以提示插入重复键,如:

db.test.insert({"userid":5})

db.test.insert({"userid":5})

E11000 duplicate key error index: test.test.$userid_1 dup key: { : 5.0 }

如果插入的文档中不包含userid键,那么该文档中该键的值为null,如果多次插入类似的文档,MongoDB将会报出同样的错误,如:

db.test.insert({"userid1":5})

db.test.insert({"userid1":5})

E11000 duplicate key error index: test.test.$userid_1 dup key: { : null }

如果在创建唯一索引时已经存在了重复项,我们可以通过下面的命令帮助我们在创建唯一索引时消除重复文档,仅保留发现的第一个文档,如:

--先删除刚刚创建的唯一索引。

db.test.dropIndex({"userid":1})

--插入测试数据,以保证集合中有重复键存在。

db.test.remove()

db.test.insert({"userid":5})

db.test.insert({"userid":5})

--创建唯一索引,并消除重复数据。

db.test.ensureIndex({"userid":1},{"unique":true,"dropDups":true})

--查询结果确认,重复的键确实在创建索引时已经被删除。

db.test.find()

{ "_id" : ObjectId("4fe823c180144abd15acd52e"), "userid" : 5 }

我们同样可以创建复合唯一索引,即保证复合键值唯一即可。如:

db.test.ensureIndex({"userid":1,"age":1},{"unique":true})

3、查询当前聚集集合所有索引

db.userInfo.getIndexes();

4、查看总索引记录大小

db.userInfo.totalIndexSize();

5、读取当前集合的所有index信息

db.users.reIndex();

6、删除指定索引

db.users.dropIndex("username":1);

7、删除所有索引索引

db.users.dropIndexes();

更多精彩内容其他人还在看

MongoDB 3.4 安装以 Windows 服务方式运行的详细步骤

这篇文章主要介绍了MongoDB 3.4 安装以 Windows 服务方式运行的详细步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB如何查询耗时记录的方法详解

查询操作是我们日常操作数据库经常会遇到的一个功能,下面这篇文章主要给大家介绍了关于MongoDB如何查询耗时记录的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

MongoDB开源数据库开发工具dbKoda

这篇文章主要介绍了MongoDB开源数据库开发工具dbKoda的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB 查询操作的实例详解

这篇文章主要介绍了MongoDB 查询操作的实例的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

java操作mongoDB查询的实例详解

这篇文章主要介绍了java操作mongo查询的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

windows7下使用MongoDB实现仓储设计

本文给大家介绍了本人在物联网环境下基于MongoDB实现的仓储的配置信息及具体的使用示例,非常的实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

关于Mongodb 认证鉴权你需要知道的一些事

最近因为mongodb被黑了,早先我就疑惑过怎么这东西开启服务就能用了,后来找了各方资料,知道MongoDB默认是没有鉴权的,所以这篇文章主要给大家介绍了关于Mongodb认证鉴权你需要知道的一些事,需要的朋友可以参考下。
收藏 0 赞 0 分享

MongoDB TTL索引的实例详解

这篇文章主要介绍了 MongoDB TTL索引的实例详解的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB如何对数组中的元素进行查询详解

MongoDB在文档上支持数组,其次数组上可以实现嵌套,以及数组元素也可以文档。所以下面这篇文章主要给大家介绍了关于MongoDB如何对数组中元素进行查询的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Mac中mongoDB的安装与卸载步骤详解

mongoDB是一个非常不错的数据库,最近也正在学习mongoDB,在使用一个数据库前必不可少的就是安装和卸载,所以下面这篇文章主要给大家介绍了关于Mac系统中mongoDB安装与卸载的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多