MongoDB 高级聚合查询
来源:互联网 发布:php最新视频教程百度云 编辑:程序博客网 时间:2024/05/21 09:49
MongoDB版本为:2.0.8
系统为:64位Ubuntu 12.04
先给他家看一下我的表结构[Oh sorry, Mongo叫集合]
如你所见,我尽量的模拟现实生活中的场景。这是一个人的实体,他有基本的manId, manName, 有朋友[myFriends],有喜欢的水果[fruits],而且每种水果都有喜欢的权重。
很不好的是你还看见了有个“_class”字段? 因为我是Java开发者, 我还喜欢用Spring,因此我选用了Spring Data Mongo的类库[也算是框架吧,但是我不这么觉得]。
现在有很多人Spring见的腻了也开始烦了。是的,Spring野心很大,他几乎想要垄断Java方面的任何事情。没办法我从使用Spring后就离不开他,以至于其他框架基本上都不用学。我学了Spring的很多,诸如:Spring Security/Spring Integration/Spring Batch等。。。不发明轮子的他已经提供了编程里的很多场景,我利用那些场景解决了工作中的很多问题,也使我的工作变得很高效。从而我又时间学到它更多。Spring Data Mongo封装了mongodb java driver,提供了和SpringJDBC/Template一致编程风格的MongoTemplate。
见:http://static.springsource.org/spring-data/data-mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html
不说废话了,我们直接来MongoDB吧。
- Max 和Min
我和同事在测试Mongo时,索引还写了不到一半,他想查询某个字段的最大值,结果找了半天文档也没找到关于max的函数。我也很纳闷这是常规函数啊怎么不提供? 后来经过翻阅资料确定Mongo确实不提供直接的max和min函数。但是可以通过间接的方式[sort 和 limit]实现这个。
要查询最大值我们只需要把结果集按照降序排列,取第一个值就是了。
如我的例子,我想取得集合中年龄最大的人。
1
db.person.
find
({}).
sort
({
"age"
: -1}).limit(1)
相反如果想要年龄最小的人,只需要把sort中改为{“age”:1}就可以了。
当然我们使用了sort,对于小数量的文档是没问题的。当对于大量数据需要给age建立索引,否则这个操作很耗时。
- distinct
如上面的图片,我认为fruits和myFriends字段是不同的。网上很多资料和例子都没说到这个情景,因为我们也业务是fruits这样的模型,我测试了。对于fruits.fruitId他也是可行的。
如上面的表结构,我想统计所有的喜欢的水果。
1
db.person.distinct(
"fruits.fruitId"
)
他成功执行了。输出如:
1
[
"aaa"
,
"bbb"
,
"ccc"
,
"www"
,
"xxx"
,
"yyy"
,
"zzz"
,
"rrr"
]
我想统计集合中共有多少个人[按名字吧]
1
db.person.distinct(
"manName"
)
我想统计指定个数的人的共同关注的朋友。
1
db.person.distinct(
"myFriends"
, {
"manName"
: {
"$in"
: [
"ZhenQin"
,
"YangYan"
]}})
输出如:
1
[
"234567"
,
"345678"
,
"456789"
,
"987654"
,
"ni"
,
"wo"
]
那么我使用Java呢? 我只是在演示Mongo的命令,用Spring Data Mongo是怎么操作的?
Spring Schema:
01
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
02
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
03
xmlns:context
=
"http://www.springframework.org/schema/context"
04
xmlns:mongo
=
"http://www.springframework.org/schema/data/mongo"
05
xsi:schemaLocation="http://www.springframework.org/schema/beans
06
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
07
http://www.springframework.org/schema/context
08
http://www.springframework.org/schema/context/spring-context-3.1.xsd
09
http://www.springframework.org/schema/data/mongo
10
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
11
12
<
context:property-placeholder
location
=
"classpath:mongo.properties"
/>
13
14
<!-- Default bean name is 'mongo' -->
15
<
mongo:mongo
id
=
"mongo"
host
=
"${mongo.host}"
port
=
"${mongo.port}"
/>
16
17
<
mongo:db-factory
id
=
"mongoDbFactory"
18
mongo-ref
=
"mongo"
19
dbname
=
"mongotest"
/>
20
21
<
bean
id
=
"mongoTemplate"
class
=
"org.springframework.data.mongodb.core.MongoTemplate"
>
22
<
constructor-arg
name
=
"mongoDbFactory"
ref
=
"mongoDbFactory"
/>
23
</
bean
>
24
</
beans
>
max和min的测试:
01
@Test
02
public
void
testMaxAndMinAge()
throws
Exception {
03
Query q =
new
BasicQuery(
"{}"
).with(
new
Sort(
new
Sort.Order(Sort.Direction.ASC,
"age"
))).limit(
1
);
04
Person result = mongoTemplate.findOne(q, Person.
class
);
05
log.info(result);
06
07
q =
new
BasicQuery(
"{}"
).with(
new
Sort(
new
Sort.Order(Sort.Direction.DESC,
"age"
))).limit(
1
);
08
result = mongoTemplate.findOne(q, Person.
class
);
09
log.info(result);
10
}
distinct的测试:
01
@Test
02
public
void
testDistinct()
throws
Exception {
03
List result = mongoTemplate.getCollection(
"person"
).distinct(
"myFriends"
);
04
for
(Object o : result) {
05
log.info(o);
06
}
07
08
log.info(
"=================================================================="
);
09
Query query = Query.query(Criteria.where(
"manId"
).is(
"123456"
));
10
result = mongoTemplate.getCollection(
"person"
).distinct(
"myFriends"
, query.getQueryObject());
11
for
(Object o : result) {
12
log.info(o);
13
}
14
15
log.info(
"=================================================================="
);
16
result = mongoTemplate.getCollection(
"person"
).distinct(
"fruits.fruitId"
);
17
for
(Object o : result) {
18
log.info(o);
19
}
20
}
输出的结果为:
01
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] 234567
02
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] 345678
03
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] 456789
04
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] 987654
05
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] ni
06
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] wo
07
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(72)] 123456
08
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(75)] ==================================================================
09
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(79)] 234567
10
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(79)] 345678
11
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(79)] 456789
12
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(79)] 987654
13
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(82)] ==================================================================
14
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] aaa
15
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] bbb
16
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] ccc
17
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] www
18
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] xxx
19
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] yyy
20
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] zzz
21
12-22 14:13:45 [INFO] [t.MongoAdvaceQueryTest(85)] rrr
22
12-22 14:13:45 [INFO] [support.GenericApplicationContext(1020)] Closing org.springframework.context.support.GenericApplicationContext@1e0a91ff: startup
date
[Sat Dec 22 14:13:44 CST 2012]; root of context hierarchy
- MongoDB 高级聚合查询
- MongoDB高级查询[聚合Group]
- MongoDB高级查询[聚合Group]
- MongoDB高级查询[聚合Group]
- Mongodb高级查询Aggregation聚合组件分页
- 二、MongoDB的高级查询(聚合、游标、管道、索引)
- MongoDB的聚合查询
- mongodb nodeJs聚合查询
- MongoDB 聚合查询
- mongodb mapreduce 聚合查询
- MongoDB聚合查询
- mongodb 高级操作:聚合,游标
- java MongoDB 聚合查询aggregation
- MongoDB查询、索引和聚合
- MongoDB Spring实现(聚合查询)
- mongodb 聚合查询每天统计
- MongoDB高级查询
- mongodb的高级查询
- Dynamic Forking of Win32 EXE
- Wireshark图解教程
- Binder
- 工厂模式与jquery
- Ubuntu 13.10(64位)安装WPS(32位)
- MongoDB 高级聚合查询
- hdoj1509 Windows Message Queue (优先队列)
- 话说你最拼命的时候怎样拼命的?
- lua 进阶3--lua文件中调用C++函数
- hdu 1158-Employment Planning,n*n*n
- 十四项神奇心理定律
- HDU 2897 邂逅明下 (博弈)
- 最省费用
- [Usaco2011 Nov]Cow Steeplechase奶牛越野跑(二分图裸题)