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 中文 API (40) —— RatingBar
Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...
- CentOS LiveCD LiveDVD DVD 等版本的区别
1.CentOS系统镜像DVD有两个,安装系统只用到第一个镜像即CentOS-6.7-x86_64-bin-DVD1.iso,第二个镜像CentOS-6.7-x86_64-bin-DVD2.iso是系 ...
- ng-camera的API详解
Camera github地址: apache/cordova-plugin-camera 安装 cordova plugin add cordoba-plugin-camera 项目中的使用
- .NET Core初体验 - 在Mac下运行第一个Web示例程序
要说最近两天程序猿之间最喜欢吹水的事是什么?那绝壁是甲骨文要放弃Java!简直做梦都要笑醒!由于公司的产品线全面转向Java,最近几个月也一直在苦学Java技术.已经默默决定了,如果消息证实是真的,我 ...
- 关于.net根目录路径的问题
今天做了一个项目,用了url重写,但是在本地目录是localhost/BK/index.aspx,而其他目录也必须带bk,不带的话就找不到页面,(iis里正常) 应该主目录去掉bk,设置方法,右击网站 ...
- C#ArrayList对象集合
ArrayList alist = new ArrayList(); //集合对像 长度可以改变,类型不限 //添加单个元素可以Add() alist.Add("在在的"); al ...
- 一个小玩具:Python调用Mysql
1. ubuntu安装MySQL how to install:$ sudo apt-get install mysql-server$ sudo apt-get install mysql-clie ...
- nginx 配置以及常用命令
windows下安装以及配置nginx http://jingyan.baidu.com/article/f3e34a12a9c1c3f5eb6535d4.html 1)下载地址: http://ng ...
- 面试题,自己写写dome总是好的
1.完成如下图所示的布局 <html> <head> <meta http-equiv="Content-Type" content="te ...
- vagrant 配置文件简析
#自定义box名字 config.vm.box = "website" #以ip192.168.33.10访问 config.vm.network "private_ne ...