[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 ...
随机推荐
- JSONObject和JSONArray(json-lib-2.4)的基本用法
json-lib-2.4是一个用于JSON和java对象间转换的第三方包,其jar和依赖包下载地址在:https://files.cnblogs.com/files/xiandedanteng/jso ...
- LogManager
public class LogManager { // Fields public static bool Debugstate; // Methods public static void Log ...
- 在 .NET 4.0 下编写扩展代码以支持 async 异步编程
微软在C# 5中引入了async.await这两个异步编程的关键字,要使用这两个关键字需要你的IDE支持C#5.0语法,也就意味着你需要使用VS 2012版本以上IDE,或者在Vs2010卸载其编译器 ...
- PHP经常使用正則表達式汇总
1. 平时做站点常常要用正則表達式,以下是一些解说和样例,仅供大家參考和改动使用: 2. "^\d+$" //非负整数(正整数 + 0) 3. "^ ...
- redis实践一些要注意的事项
不要放垃圾数据,及时清理无用数据实验性的数据和下线的业务数据及时删除; key尽量都设置过期时间对具有时效性的key设置过期时间,通过redis自身的过期key清理策略来降低过期key对于内存的占用, ...
- ARM(CM3)的汇编指令
转http://blog.csdn.net/gaojinshan/article/details/11534569 16位数据操作指令 名字 功能ADC 带进位加法(ADD with Carry) ...
- php学习网站推荐
http://nikic.github.io/https://github.com/walu/phpbook http://www.chinaunix.net/ https://news.ycombi ...
- 喜闻乐见的const int *p、int* const p、const int* const p
不废话直接代码示例: void f(const int *p) { ; *p = ; // error p = &b; // fine } void f(int* const p) { ; * ...
- GNU_MAKE--工程管理
GNU MAKE--工程管理 makefile是为工程组织编译,为“自动化编译”,一旦写成,只需要一个make命令,整个工程完全自动编译,极大提高了软件开发效率.make是一个命令工具,是一个解释ma ...
- Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理
/** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...