http://docs.spring.io/spring-data/data-mongo/docs/1.0.0.M5/api/org/springframework/data/mongodb/core/MongoTemplate.html 在线api文档

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 }}
 
2注解方式

2.1查询所有属性
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使用

使用后如图,不存储对象数据,只存储 集合名和id 

spring-data-mongodb必须了解的操作的更多相关文章

  1. Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)

    一.简介 Spring Data  MongoDB 项目提供与MongoDB文档数据库的集成.Spring Data MongoDB POJO的关键功能区域为中心的模型与MongoDB的DBColle ...

  2. 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南

    1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...

  3. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)

    一.简单介绍 Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...

  4. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  5. Spring Data MongoDB 四:基本文档改动(update)(一)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 二:MongoDB加入.删除.改动 一.简单介绍 Spring Data  Mo ...

  6. Spring Data MongoDB 五:进阶文档查询(分页、Morphia)(二)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) 一.简单介绍 Spring ...

  7. 使用Spring访问Mongodb的方法大全——Spring Data MongoDB

    1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...

  8. spring data mongodb CURD

    一.添加 Spring  Data  MongoDB 的MongoTemplate提供了两种存储文档方式,分别是save和insert方法,这两种的区别: (1)save :我们在新增文档时,如果有一 ...

  9. 使用Spring Data Mongodb的MongoRepository类进行增删改查

    Spring Data Mongodb提供一套快捷操作 mongodb的方法,创建Dao,继承MongoRepository,并指定实体类型和主键类型. public interface CmsPag ...

  10. 如何在Spring Data MongoDB 中保存和查询动态字段

    原文: https://stackoverflow.com/questions/46466562/how-to-save-and-query-dynamic-fields-in-spring-data ...

随机推荐

  1. php中定界符<<<的用法

    定界符给字符串定界的方法使用定界符语法(“<<<”).应该在 <<< 之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串. 结束标识符必须从行的第一列开 ...

  2. linux系统自动执行任务(转)

    开机启动 开机启动应该是我们很经常的需求了,我们常需要在开机时就自动执行某些命令来开启服务,进程等,有了它我们不必再在每次开机时输入同一堆命令. chkconfig命令 使用chkconfig命令可以 ...

  3. Multipatch对象

    Multipatch对象是 TriangleStrip 和TriangleFan, Trangle,Ring对象的集合 TriangleStrip TriangleFan Trangle

  4. SqlServer 开启或关闭数据库主键自增

    可用作删除一行主键数据库,在还原数据行,或者删掉后,被伤处的主键还可以利用 --开启当前表的可复制功能,仅在当前回话中有效 SET IDENTITY_INSERT  dbo.PDAUserInfo O ...

  5. ORACLE中使用SQL的正则表达式判断邮箱格式

    在数据库中,有时需要判断字符串是否是一个或者多个邮箱格式,可以使用如下语句判断: ) FROM dual WHERE regexp_like(v_mail,'^\w+((-\w+)|(\.\w+))* ...

  6. jQuery事件与事件对象

    事件是脚本编程的灵魂,本篇来介绍jQuery中的事件处理及事件对象. 事件与事件对象 首先,我们来看一下经常使用的添加事件的方式: <input type="button" ...

  7. O、Ω、Θ表示

    转载,原网址为:http://book.2cto.com/201211/8127.html 对于任何数学函数,这三个记号可以用来度量其“渐近表现”,即当趋于无穷大时的阶的情况,这是算法分析中非常重要的 ...

  8. php 之 数据访问 增删改查练习题

    练习题内容: 一.查看新闻页面-----主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  9. HTML5比较实用的代码

    增强IE兼容性 <!--[if IE]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js&q ...

  10. set{变量 = value;}get{return 变量;}

    形如: public string _customValue        {            set { _customValue = value; }            get { re ...