[Spring Data MongoDB]学习笔记--MongoTemplate查询操作
查询操作主要用到两个类:Query, Criteria
所有的find方法都需要一个query的object。
1. 直接通过json来查找,不过这种方式在代码中是不推荐的。
BasicQuery query = new BasicQuery("{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}");
List<Person> result = mongoTemplate.find(query, Person.class);
2. 推荐使用where + query的方式来进行查找。
where方法生成一个Criteria对象,然后可以通过调用不同的方法增加操作符(比如lt,gt,and)。
更详细的操作列表请参考http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.RELEASE/reference/html/mongo.core.html
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query; … List<Person> result = mongoTemplate.find(query(where("age").lt(50)
.and("accounts.balance").gt(1000.00d)), Person.class);
3. MongoDB也支持空间查询,比如附近的点,下面只给出例子,详细请看官方文档。
@Document(collection="newyork")
public class Venue { @Id
private String id;
private String name;
private double[] location; @PersistenceConstructor
Venue(String name, double[] location) {
super();
this.name = name;
this.location = location;
} public Venue(String name, double x, double y) {
super();
this.name = name;
this.location = new double[] { x, y };
} public String getName() {
return name;
} public double[] getLocation() {
return location;
} @Override
public String toString() {
return "Venue [id=" + id + ", name=" + name + ", location="
+ Arrays.toString(location) + "]";
}
}
查找圆内的地址
Circle circle = new Circle(-73.99171, 40.738868, 0.01);
List<Venue> venues =
template.find(new Query(Criteria.where("location").withinCenter(circle)), Venue.class);
查找球面坐标内的地址
Circle circle = new Circle(-73.99171, 40.738868, 0.003712240453784);
List<Venue> venues =
template.find(new Query(Criteria.where("location").withinCenterSphere(circle)), Venue.class);
[Spring Data MongoDB]学习笔记--MongoTemplate查询操作的更多相关文章
- [Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作
插入操作: 直接给个例子 import static org.springframework.data.mongodb.core.query.Criteria.where; import static ...
- [Spring Data MongoDB]学习笔记--牛逼的MongoTemplate
MongoTemplate是数据库和代码之间的接口,对数据库的操作都在它里面. 注:MongoTemplate是线程安全的. MongoTemplate实现了interface MongoOperat ...
- [Spring Data MongoDB]学习笔记--_id和类型映射
_id字段的映射: MongoDB要求所有的document都要有一个_id的字段. 如果我们在使用中没有传入_id字段,它会自己创建一个ObjectId. { , "accounts&qu ...
- [Spring Data MongoDB]学习笔记--建立数据库的连接
1. 有了上一篇的Mongo后,连接数据库我们还需要更多的信息,比如数据库名字,用户名和密码等. 我们可以继续来配置MongoDbFactory的实例. public interface MongoD ...
- [Spring Data MongoDB]学习笔记--MapReduce
mongodb的MapReduce主要包含两个方法:map和reduce. 举个例子,假设现在有下面3条记录 { "_id" : ObjectId("4e5ff893c0 ...
- [Spring Data MongoDB]学习笔记--注册一个Mongo实例
1. 通过Java based bean metadata @Configuration public class AppConfig { public @Bean Mongo mongo() thr ...
- 如何在Spring Data MongoDB 中保存和查询动态字段
原文: https://stackoverflow.com/questions/46466562/how-to-save-and-query-dynamic-fields-in-spring-data ...
- 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分
Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...
- [Spring Data Repositories]学习笔记--使用现有的repository
以下内容是在学习Spring-Data-mongoDB中的Spring Data Repositories时做的一些笔记.备忘! 感觉学习还是看官方的资料比较透彻一些. Spring Data Rep ...
随机推荐
- Intellij IDEA 2017 破解
http://idea.lanyus.com/ https://www.cnblogs.com/wang1024/p/7485758.html
- lodash map
_.map(collection, [iteratee=_.identity]) 创建一个经过 iteratee 处理的集合中每一个元素的结果数组. iteratee 会传入3个参数:(value, ...
- Linux Ubuntu 开机自动启动项设置方法 例:svn服务
在init.d目录建立一个脚本文件svnd.sh # cd /etc/init.d # vim svnd.sh 输入svnd.sh内容如下(/kaifa/svn 为svn仓库目录): #!/bin/b ...
- 复选框input:checkbox
复选框 CreateTime--2017年6月5日14:04:55Author:Marydon 五.复选框 (一)语法 <input type="checkbox" /& ...
- OpenStack-Heat中的AWS::WaitCondition的使用
在heat中.一个instance的创建成功信号是在这个instance状态成为active之后发出的,这时候user-data可能还没有运行.可是heat已经觉得这个resource创建成功了,開始 ...
- AIDL调用指南
近期有需求要实现两个apk之间的通信,想到用AIDL来实现,现写一个demo学习下AIDL怎样使用. 这里我要实现一个apk(client端)调用还有一个apk(server端)的方法. 先实现ser ...
- SSH——增删改的实现二
二.批量删除 逻辑删除取派员,将取派员的deltag改为“1” 1. 为“作废”按钮绑定事件 //批量删除取派员 function doDelete(){ //获得选中的行 var rows = $( ...
- nnlog-yaml
from nnlog import Logger# log=Logger(file_name='my.log',level='debug',# when='S',backCount=5,interva ...
- git 命令使用速查手册( 个人版)
1. 克隆远程库 git clone repository_address 通过 git clone 获取的git库只是远程库中的当前工作分支,如果想获取其它分支信息,可参考下面. 2. 查看远程 ...
- php 实现 html转js
[php] <?php function htmltojs($str){ $re=''; $str=str_replace('\','\\',$str); $str=str_replace(&q ...