[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 ...
随机推荐
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- 使用IntelliJ IDEA创建Maven多模块项目
转载:http://blog.csdn.net/xyw591238/article/details/52794788 使用Maven管理项目时,往往需要创建多个模块,模块之间存在相互引用的关系.对于M ...
- RAID详解[RAID0/RAID1/RAID10/RAID5] (转)
一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...
- 【DB2】经典SQL写法
1.环境准备 CREATE TABLE DataInfo( ID_1 ), ID_2 ) ) INSERT INTO DataInfo VALUES('A','Oracle'); INSERT INT ...
- Refactoring之——代码的坏味道(一)过长方法
1 代码的坏味道 重构一书中提到了22种代码的坏味道,大致可以分为几类. 识别代码的坏味道,有助于发现代码的潜在问题,从而可以有的放矢的修改现有代码,使之不断完善. 1.1 Bloaters(臭鲱,暂 ...
- 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器
1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...
- nginx php 使用unix socket 还是tcp?
两种通信方式的分析和总结 从原理上来说,unix socket方式肯定要比tcp的方式快而且消耗资源少,因为socket之间在nginx和php-fpm的进程之间通信,而tcp需要经过本地回环驱动,还 ...
- 转: android emulator 命令详解
在命令行输入: emulator -help,即可显示emulator支持的所有命令. Android Emulator usage: emulator [options] [-qemu args] ...
- Atitit.aticmd v4 新特性q39 添加定时器释放功能
Atitit.aticmd v4 新特性q39 添加定时器释放功能 V1 实现兰cmd V2 标准输入,标准输出,标准错误与重新定向 V3 stdout stderr统一重新定向 V4 添加定 ...
- Atitit.自定义jdbc驱动 支持jsql
Atitit.自定义jdbc驱动 支持jsql 1. 为什么需要自定义驱动1 1.1. 透明分库分表1 1.2. 自定义数据库的接口.比如大数据文档文件类型的数据库,数据存储引擎2 2. 整个文章分 ...