如你所见,我尽量的模拟现实生活中的场景。这是一个人的实体,他有基本的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吧。
我和同事在测试Mongo时,索引还写了不到一半,他想查询某个字段的最大值,结果找了半天文档也没找到关于max的函数。我也很纳闷这是常规函数啊怎么不提供? 后来经过翻阅资料确定Mongo确实不提供直接的max和min函数。但是可以通过间接的方式[sort 和 limit]实现这个。
要查询最大值我们只需要把结果集按照降序排列,取第一个值就是了。
如我的例子,我想取得集合中年龄最大的人。
1 | db.person. find ({}). sort ({ "age" : -1}).limit(1) |
相反如果想要年龄最小的人,只需要把sort中改为{“age”:1}就可以了。
当然我们使用了sort,对于小数量的文档是没问题的。当对于大量数据需要给age建立索引,否则这个操作很耗时。
MongoDB的destinct命令是获取特定字段中不同值列表的最简单工具。该命令适用于普通字段,数组字段[myFriends]和数组内嵌文档[fruits].
如上面的图片,我认为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"> |
12 | < context:property-placeholder location = "classpath:mongo.properties" /> |
14 | <!-- Default bean name is 'mongo' --> |
15 | < mongo:mongo id = "mongo" host = "${mongo.host}" port = "${mongo.port}" /> |
17 | < mongo:db-factory id = "mongoDbFactory" |
21 | < bean id = "mongoTemplate" class = "org.springframework.data.mongodb.core.MongoTemplate" > |
22 | < constructor-arg name = "mongoDbFactory" ref = "mongoDbFactory" /> |
max和min的测试:
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 ); |
07 | q = new BasicQuery( "{}" ).with( new Sort( new Sort.Order(Sort.Direction.DESC, "age" ))).limit( 1 ); |
08 | result = mongoTemplate.findOne(q, Person. class ); |
distinct的测试:
02 | public void testDistinct() throws Exception { |
03 | List result = mongoTemplate.getCollection( "person" ).distinct( "myFriends" ); |
04 | for (Object o : result) { |
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) { |
15 | log.info( "==================================================================" ); |
16 | result = mongoTemplate.getCollection( "person" ).distinct( "fruits.fruitId" ); |
17 | for (Object o : result) { |
输出的结果为:
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 |
这里我要特别说明一下, 当使用了Spring Data Mongo,如上面的findOne(query, Person.class)它就会把查询的结果集转换成Person类的对象。Spring Data Mongo的很多API中都这样,让传入了一个Bean的class对象。因为distinct的测试是输出list<String>的,我使用的mongo-java-driver的api。他们都很简单,唯一的是Query这个Spring提供的对象,希望读者注意,他几乎封装了所有条件查询,sort,limit等信息。query的api见: