spring-data-mongodb必须了解的操作
1关键之识别
| Keyword | Sample | Logical result |
| GreaterThan | findByAgeGreaterThan(int age) | {"age" : {"$gt" : age}} |
| LessThan | findByAgeLessThan(int age) | {"age" : {"$lt" : age}} |
| Between | findByAgeBetween(int from, int to) | {"age" : {"$gt" : from, "$lt" : to}} |
| IsNotNull, NotNull | findByFirstnameNotNull() | {"age" : {"$ne" : null}} |
| IsNull, Null | findByFirstnameNull() | {"age" : null} |
| Like | findByFirstnameLike(String name) | {"age" : age} ( age as regex) |
| Regex | findByFirstnameRegex(String firstname) | {"firstname" : {"$regex" : firstname }} |
| (No keyword) | findByFirstname(String name) | {"age" : name} |
| Not | findByFirstnameNot(String name) | {"age" : {"$ne" : name}} |
| Near | findByLocationNear(Point point) | {"location" : {"$near" : [x,y]}} |
| Within | findByLocationWithin(Circle circle) | {"location" : {"$within" : {"$center" : [ [x, y], distance]}}} |
| Within | findByLocationWithin(Box box) | {"location" : {"$within" : {"$box" : [ [x1, y1], x2, y2]}}}True |
| IsTrue, True | findByActiveIsTrue() | {"active" : true} |
| IsFalse, False | findByActiveIsFalse() | {"active" : false} |
| Exists | findByLocationExists(boolean exists) | {"location" : {"$exists" : exists }} |
publicinterfacePersonRepositoryextendsMongoRepository<Person,String>
@Query("{ 'firstname' : ?0 }")
List<Person> findByThePersonsFirstname(String firstname);
}
2.2查询部分属性
publicinterfacePersonRepositoryextendsMongoRepository<Person,String>
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
List<Person> findByThePersonsFirstname(String firstname);
}
3bean的配置属性
@Id- 配置id@Document映射到数据库的集合名,可以设置名称@DBRef- applied at the field to indicate it is to be stored using a com.mongodb.DBRef.@Indexed- applied at the field level to describe how to index the field.@CompoundIndex- applied at the type level to declare Compound Indexes@GeoSpatialIndexed- applied at the field level to describe how to geoindex the field.@Transient- 当有数据部需要保存的时候可以使用@PersistenceConstructor- marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject.@Value- this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object.@Field- 给该属性添加存储在数据库中的名字@Document
@CompoundIndexes({
@CompoundIndex(name ="age_idx",def="{'lastName': 1, 'age': -1}")
})//上面配置了联合属性lastName和age
publicclassPerson<T extendsAddress>{ @Id
privateString id; @Indexed(unique =true)
privateInteger ssn; @Field("fName")
privateString firstName; @Indexed
privateString lastName; privateInteger age; @Transient
privateInteger accountTotal; @DBRef
privateList<Account> accounts; private T address; publicPerson(Integer ssn){
this.ssn = ssn;
} @PersistenceConstructor
publicPerson(Integer ssn,String firstName,String lastName,Integer age, T address){
this.ssn = ssn;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.address = address;
}
}
3.2 @DBRef使用

spring-data-mongodb必须了解的操作的更多相关文章
- Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)
一.简介 Spring Data MongoDB 项目提供与MongoDB文档数据库的集成.Spring Data MongoDB POJO的关键功能区域为中心的模型与MongoDB的DBColle ...
- 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南
1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)
一.简单介绍 Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery
一.简介 spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...
- Spring Data MongoDB 四:基本文档改动(update)(一)
Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 二:MongoDB加入.删除.改动 一.简单介绍 Spring Data Mo ...
- Spring Data MongoDB 五:进阶文档查询(分页、Morphia)(二)
Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) 一.简单介绍 Spring ...
- 使用Spring访问Mongodb的方法大全——Spring Data MongoDB
1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...
- spring data mongodb CURD
一.添加 Spring Data MongoDB 的MongoTemplate提供了两种存储文档方式,分别是save和insert方法,这两种的区别: (1)save :我们在新增文档时,如果有一 ...
- 使用Spring Data Mongodb的MongoRepository类进行增删改查
Spring Data Mongodb提供一套快捷操作 mongodb的方法,创建Dao,继承MongoRepository,并指定实体类型和主键类型. public interface CmsPag ...
- 如何在Spring Data MongoDB 中保存和查询动态字段
原文: https://stackoverflow.com/questions/46466562/how-to-save-and-query-dynamic-fields-in-spring-data ...
随机推荐
- Android 使用新浪微博SSO授权
新浪微博SSO授权,很早就做好了,只是一直没有时间整理博客,今天加班,晚上闲暇之时便想到整理一下.由于整个七月份很忙,加班很多.前段时间把腾讯微博的SSO认证整理好了.想在七月份翻篇之前再写点东西.好 ...
- 内容提供者 ContentResolver 数据库 示例 -2
MainActivity public class MainActivity extends ListActivity { // 访问内容提供者时需要的主机名称 public stat ...
- NumberBox( 数值输入框) 组件
本节课重点了解 EasyUI 中 NumberBox(数值输入框)组件的使用方法,这个组件依赖于 ValidateBox(验证框)组件.一. 加载方式//class 加载方式<input typ ...
- 3.1,pandas【基本功能】
一:改变索引 reindex方法对于Series直接索引,对于DataFrame既可以改变行索引,也可以改变列索引,还可以两个一起改变. 1)对于Series In [2]: seri = pd.Se ...
- 使用Gird++打印出现“Retrieving the COM class factory for component with CLSID”的解决办法
我们的接口需要返回一个gird++生成PDF文件的二进制数据,在本地测试都很好,发布到服务器上一直出现“Retrieving the COM class factory for component w ...
- 修改Sharepoint 文档库列表点击Excel文件默认跳转到Excel Service服务 xlviewer.aspx页面
在Sharepoint 文档库中,当点击库中的一个Excel文件时,Sharepoint默认为转跳到Excel Services上,无论是Sharepoint 的是否开启了Excel Service, ...
- reason: 'Could not instantiate class named MKMapView'
详细原因: 拖入MapView直接运行,结果报错 解决:在Xcode导入MapKit.framework既可
- (转)OS X 升級後 MacPorts 重新安裝筆記
原地址:http://blog.lyhdev.com/2012/07/os-x-macports.html Mac OS X 10.8 Mountain Lion 正式發佈,而且祭出台幣 $590 元 ...
- 菜鸟做HTML5小游戏 - 翻翻乐
记录下开放过程.做小游戏开发,又要跨平台,flash又不支持iPhone,html5是最好的选择. 先看看最后效果: 好了,开始demo. 1.准备工作: 图片素材(省略...最后代码一起打包) 了解 ...
- Android再学习-20141023-Intent-Thread
20141023-Android再学习 Intent对象的基本概念 Intent是Android应用程序组件之一 Intent对象在Android系统中表示一种意图 Intent当中最重要的内容是ac ...