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. Swift开放StatsD后上传数据的出现,出现退换货503的Bug

    转载请注明出处:http://blog.csdn.net/cywosp/article/details/40781569 swift在版本号2.1.0之前假设各个服务的配置文件里打开下面配置后,且系统 ...

  2. .NET基础拾遗(7)多线程开发基础1

    一.多线程编程的基本概念 1.1 操作系统层面的进程和线程 (1)进程 进程代表了操作系统上运行着的一个应用程序.进程拥有自己的程序块,拥有独占的资源和数据且可以被操作系统调度. But,即使是同一个 ...

  3. mvc自带的异步表单提交

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. C# 还原SQL数据库(非存储过程方式)

    Winform的代码,最好是在数据所在电脑上运行代码,不然会因为权限问题导致失败. 数据库备份: SqlConnection con = new SqlConnection("Data So ...

  5. 【socket.io研究】0.前提准备

    WebSocket出现之前,web实时推送,一般采用轮询和Comet技术(可细分为长轮询机制和流技术两种),需要大量http请求,服务器受不了.HTML5定义了WebSocket协议,基于TCP协议, ...

  6. angularjs深入理解向指令传递数据,双向绑定机制

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  7. hdu5391 Zball in Tina Town(威尔逊定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Zball in Tina Town Time Limit: 3000/1500 ...

  8. uva 498 - Polly the Polynomial

    UVa 498: Polly the Polynomial | MathBlog #include <cstdio> #include <cstdlib> using name ...

  9. git使用笔记(三)(图文说明) 图解提交更改内容的不同方式,涉及代码

    此步之前的工作和示例请参考以下帖子: git使用笔记(一)Git的下载与配置 git使用笔记(二) 如何把GitHub上项目同步到本地 -------------------------------- ...

  10. Android中写入读取XML

    获取XML文件的基本思路是,通过getResources().getXml()获的XML原始文件,得到XmlResourceParser对象,通过该对象来判断是文档的开头还是结尾,是某个标签的开始还是 ...