丰富的查询
MongoDB支持类似于SQL的>,>=,<,<=,<>,分别用 "$gt", "$gte", "$lt", "$lte", "$ne"表示,以及:$in,$nin,也支持条件的组合:and, or。
甚至还支持正则表达式,你没看错,正则表达式,强大吧。
> db.user.find()
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
{ "_id" : ObjectId("51f7312b04f4bacbc87f67f3"), "name" : "allen", "age" : 20, "address" : { "country" : "US", "city" : "NewYork" }, "email" : "allen@hotmail.com" }
>
> db.user.find({"age":{$lt:30}})
{ "_id" : ObjectId("51f7312b04f4bacbc87f67f3"), "name" : "allen", "age" : 20, "address" : { "country" : "US", "city" : "NewYork" }, "email" : "allen@hotmail.com" }
>
> db.user.find({"address.country":{$in:["CN","US"]},"age":{$gte:30}})
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
>
> db.user.find({"email":/.+@yahoo.com/})
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
>
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
{ "_id" : ObjectId("51f7312b04f4bacbc87f67f3"), "name" : "allen", "age" : 20, "address" : { "country" : "US", "city" : "NewYork" }, "email" : "allen@hotmail.com" }
>
> db.user.find({"age":{$lt:30}})
{ "_id" : ObjectId("51f7312b04f4bacbc87f67f3"), "name" : "allen", "age" : 20, "address" : { "country" : "US", "city" : "NewYork" }, "email" : "allen@hotmail.com" }
>
> db.user.find({"address.country":{$in:["CN","US"]},"age":{$gte:30}})
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
>
> db.user.find({"email":/.+@yahoo.com/})
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
>
另外MongoDB也提供了where子句,可以定义一个javascipt的predict函数来查询
> db.user.find({$where:function() {return this.address.city == 'Macao'}})
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
>
{ "_id" : ObjectId("51f730c604f4bacbc87f67f2"), "name" : "jack", "age" : 53, "address" : { "country" : "CN", "city" : "Macao" }, "fav" : [ "apple", "banana" ], "email" : "yeah@yahoo.com" }
>
聚集函数
MongoDB也支持常用的聚集函数:count,distinct,group等
其中group较为复杂,需要指定初始值,和$reduce函数,
另外group还可以加condition指定过滤条件,finalize指定每组文档$reduce完成后的动作
> db.user.find()
{ "_id" : ObjectId("51f7560004f4bacbc87f67f4"), "name" : "brodie", "age" : 30, "gender" : "male", "address" : { "country" : "CN", "city" : "ShangHai" } }
{ "_id" : ObjectId("51f7562304f4bacbc87f67f5"), "name" : "dany", "age" : 28, "gender" : "male", "address" : { "country" : "CN", "city" : "ShangHai" } }
{ "_id" : ObjectId("51f7563d04f4bacbc87f67f6"), "name" : "davis", "age" : 31, "gender" : "male", "address" : { "country" : "CN", "city" : "WuHan" } }
{ "_id" : ObjectId("51f7566904f4bacbc87f67f7"), "name" : "jameson", "age" : 37, "gender" : "female", "address" : { "country" : "US", "city" : "NewYork" } }
>
> db.user.count({"age":{$gte:30}})
3
> db.user.distinct("address.country")
[ "CN", "US" ]
>
> db.user.group({
... "key":{"address.country":true},
... "initial":{"users":[]},
... "condition":{"age":{$gte:30}},
... "reduce":function(curdoc, predoc) {
... predoc.users.push(curdoc.name);
... },
... "finalize":function(predoc) {
... predoc.count = predoc.users.length;
... }
... })
[
{
"address.country" : "CN",
"users" : [
"brodie",
"davis"
],
"count" : 2
},
{
"address.country" : "US",
"users" : [
"jameson"
],
"count" : 1
}
]
{ "_id" : ObjectId("51f7560004f4bacbc87f67f4"), "name" : "brodie", "age" : 30, "gender" : "male", "address" : { "country" : "CN", "city" : "ShangHai" } }
{ "_id" : ObjectId("51f7562304f4bacbc87f67f5"), "name" : "dany", "age" : 28, "gender" : "male", "address" : { "country" : "CN", "city" : "ShangHai" } }
{ "_id" : ObjectId("51f7563d04f4bacbc87f67f6"), "name" : "davis", "age" : 31, "gender" : "male", "address" : { "country" : "CN", "city" : "WuHan" } }
{ "_id" : ObjectId("51f7566904f4bacbc87f67f7"), "name" : "jameson", "age" : 37, "gender" : "female", "address" : { "country" : "US", "city" : "NewYork" } }
>
> db.user.count({"age":{$gte:30}})
3
> db.user.distinct("address.country")
[ "CN", "US" ]
>
> db.user.group({
... "key":{"address.country":true},
... "initial":{"users":[]},
... "condition":{"age":{$gte:30}},
... "reduce":function(curdoc, predoc) {
... predoc.users.push(curdoc.name);
... },
... "finalize":function(predoc) {
... predoc.count = predoc.users.length;
... }
... })
[
{
"address.country" : "CN",
"users" : [
"brodie",
"davis"
],
"count" : 2
},
{
"address.country" : "US",
"users" : [
"jameson"
],
"count" : 1
}
]
MapReduce
MapReduce是比较复杂的聚集函数,但更加灵活。
map函数:指定的分组函数,其中调用emit分组,第一个参数为分组的键,value为reduce时需要的字段;
reduce函数:分组后的处理函数,参数key为分组的key,values为分组后的集合;
mapReduce的第三个参数指定输出的结合名称;
运行mapReduce后会输出执行的状态信息
下面用mapReduce作出上面group同样的效果
> var map = function() { ... emit(this.address.country, {user:this.name,age:this.age,count:1}); ... } > > var reduce = function(key, values) { ... var result = {users:[],count:0}; ... for (var i = 0; i < values.length; ++i) { ... if (values[i].age >= 30) { ... result.users.push(values[i].user); ... result.count += values[i].count; ... } ... } ... return result; ... } > > db.user.mapReduce(map,reduce, {"out":"user_by_country"}) { "result" : "user_by_country", "timeMillis" : 62, // time used "counts" : { "input" : 4, // number of input document "emit" : 4, // times of emit called "reduce" : 1, // times reduce called,note:a key that has only a single value, will not trigger the reduce "output" : 2 }, "ok" : 1, } > db.user_by_country.find() { "_id" : "CN", "value" : { "users" : [ "brodie", "davis" ], "count" : 2 } } { "_id" : "US", "value" : { "user" : "jameson", "age" : 37, "count" : 1 } } >
相关推荐
本手册深入浅出地介绍了 MongoDB 的核心概念、查询语言、操作管理以及丰富的参考资料,适合对 MongoDB 有学习需求的人员。 MongoDB 有两种主要版本:社区版和企业版。社区版是免费的开源软件,适合个人和小型团队...
本篇文章将深入探讨MongoDB的核心概念、功能以及如何利用这两本书籍——《深入学习MongoDB》和《MongoDB权威指南(第2版)》来提升你的MongoDB技能。 首先,MongoDB的基础知识包括它的NoSQL数据模型。不同于传统的...
在本套“mongodb安装资料.rar”中,包含了从基础安装到高级部署的全面指南,特别是关于分布式数据库和Linux环境下的部署。以下是这些资料中涵盖的关键知识点: 1. **MongoDB的安装**:在Linux系统中,MongoDB的安装...
### MongoDB in Action 第二版(2016.3)知识点概述 #### 一、书籍基本信息 - **书名**:《MongoDB in Action》...无论是对于希望学习MongoDB的新手还是寻求提升技能的专业人士来说,这本书都是不可多得的学习资源。
MongoDB提供了强大的查询语言——MQL(MongoDB Query Language),支持丰富的查询表达式和聚合框架。你可以根据字段、条件进行查询,并使用正则表达式、数组过滤等高级特性。 在性能方面,MongoDB支持索引,可以...
了解这些基本信息后,你可以进一步探索MongoDB的复制集、分片集群、数据模型设计、查询优化、安全策略等高级主题,以便更好地利用MongoDB的强大功能。对于开发者而言,学习使用MongoDB的驱动程序(如Node.js、Python...
总的来说,这三本书籍为读者提供了全面的MongoDB学习资源,从基础概念到高级应用场景,帮助开发者和数据工程师充分利用MongoDB的强大功能,解决实际问题,构建高效、可扩展的数据存储解决方案。无论是初学者还是经验...
在C#中,为了与MongoDB进行交互,我们需要引入官方提供的驱动程序——MongoDB.Driver。这个"引用dll的压缩包"可能包含了这个驱动程序的DLL文件,例如`MongoDB.Driver.dll`。你需要将其添加到你的项目引用中,以便...
Java全能学习面试手册——Java面试题库.zip 01 7道消息队列ActiveMQ面试题!.pdf 02 10道Java高级必备的Netty面试题!.pdf 03 10道Java面试必备的设计模式面试题!.pdf 04 10个Java经典的List面试题!.pdf 05 10个...
- **命令行工具**:MongoDB自带了一个强大的命令行工具——JavaScript Shell(mongo shell),通过它可以直接执行查询、更新等操作。 - **交互式环境**:JavaScript Shell提供了一个交互式的环境,开发者可以通过...
14. **搜索与检索**(Searching and Retrieving):这部分内容可能会讲解如何在MongoDB中进行高效的查询操作,包括使用基本查询语句、聚合框架以及全文搜索等高级功能。 15. **锁定机制**(Locking):这部分可能会...
索引操作 8天学通MongoDB——第三天 细说高级操作 8天学通MongoDB——第二天 细说增删查改 8天学通MongoDB——第一天 基础入门 UML系列(4)团队沟通利器之UML——类图 团队沟通利器之UML—— 序列图 团队沟通利器之...
本手册主要聚焦于PHP与两种NoSQL数据库——MongoDB和Redis的集成应用,旨在为学习者提供深入的理论知识和实践指导。 首先,PHP与MongoDB的结合是现代Web开发中的常见场景。MongoDB是一种基于分布式文件存储的开源...
本书将深入介绍Hibernate的核心概念,如配置、实体管理、持久化、查询语言(HQL)以及事务处理,同时也会涵盖一些高级主题,如缓存策略和性能优化。 MongoDB则是一种非关系型数据库,以它的灵活性、高可扩展性和对...
### MongoDB4.0从入门到实践 ...通过以上八个章节的详细介绍,我们可以全面地掌握MongoDB从基础到高级的各种技能点,为后续的工作和学习打下坚实的基础。无论是初学者还是有一定经验的开发者,都能够从中受益匪浅。