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和agepublicclassPerson<T extendsAddress>{
    
    @IdprivateString id;
    
    @Indexed(unique =true)privateInteger ssn;
    
    @Field("fName")privateString firstName;
    
    @IndexedprivateString lastName;
    
    privateInteger age;
    
    @TransientprivateInteger accountTotal;
    
    @DBRefprivateList<Account> accounts;
    
    private T address;
    
    publicPerson(Integer ssn){this.ssn = ssn;}
    
    @PersistenceConstructorpublicPerson(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 

转载于:https://www.cnblogs.com/fx2008/p/3582378.html

spring-data-mongodb必须了解的操作相关推荐

  1. mongodb数据库java接口,MongoDB —— 使用Spring Data MongoDB操作数据库

    我们使用Spring Data MongoDB可以方便的在Spring boot项目中操作MongoDB 文档地址:https://docs.spring.io/spring-boot/docs/2. ...

  2. mongo java 注解,在Java中使用Spring Data MongoDB操作Mong | zifangsky的个人博客

    前言:在上一篇文章中(PS:https://www.zifangsky.cn/923.html)我简单介绍了如何在Linux中安装MongoDB以及MongoDB的增删改查等基本命令用法(PS:更多M ...

  3. SpringBoot 集成 Spring Data Mongodb 操作 MongoDB 详解

    一.MongoDB 简介 MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,且与关系数据库的最为相像的.它支持的数据结构非常松散,是类似 json 的 bso ...

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

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

  5. Spring Data MongoDB教程

    在当今世界,尽快启动并运行应用程序非常重要. 该应用程序还应该易于开发和维护. Spring是这样的框架,它提供了与许多不同框架的集成的简便性,这使得使用Spring开发应用程序变得容易. 一种这样的 ...

  6. 数据聚合Spring Data MongoDB:嵌套结果

    1引言 在上一篇文章中,我们构建了聚合管道的基本示例. 如果您需要有关如何创建项目和配置应用程序的更多详细信息,也许您想看看使用Spring Data MongoDB和Spring Boot进行数据聚 ...

  7. Spring Data MongoDB级联保存在DBRef对象上

    默认情况下, Spring Data MongoDB不支持对带有@DBRef注释的引用对象的级联操作,如引用所述 : 映射框架不处理级联保存 . 如果更改了Person对象引用的Account对象,则 ...

  8. 使用NoSQLUnit测试Spring Data MongoDB应用程序

    Spring Data MongoDB是Spring Data项目中的项目,它提供了Spring编程模型的扩展,用于编写使用MongoDB作为数据库的应用程序. 要使用NoSQLUnit为Spring ...

  9. Spring Data MongoDB示例

    Spring Data MongoDB示例 欢迎使用Spring Data MongoDB示例.Spring Data MongoDB是将Spring Framework与最广泛使用的NoSQL数据库 ...

  10. spring data mongodb CURD

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

最新文章

  1. Php 获取xml中的节点值
  2. Channel的几种状态
  3. java报错误设置属性值_java – 设置属性值时出错;嵌套异常是org.springframework.beans.NotWritablePropertyException:...
  4. 一个简单的防爬虫脚本(转载欧彬)
  5. pytorch线性回归_PyTorch中的线性回归
  6. Centos7 amp;amp; Docker amp;amp; Jenkins amp;amp; ASP.NET Core
  7. python matplotlib_高效使用 Python 可视化工具 Matplotlib
  8. 如何写好学术期刊文章?
  9. leetcode 之Rotate List(18)
  10. 计算机显卡驱动不匹配,显卡驱动不兼容怎么办 显卡驱动降低旧版本方法
  11. 【跨域问题】springBoot + VUE解决跨域问题几种处理方案
  12. video标签和source标签
  13. cad计算机绘图基础知识,机械零件制图识图必须懂的七大基础知识
  14. Kylin RT OLAP reassign流程即重新分配replica_set 流程
  15. ligerui combobox ajax,ligerui给ligerForm中的ligerComboBox添加事件
  16. 2022漂亮有质感的SummerAdmin后台模板+Layui内核
  17. kedacom摄像头怎么预置_科达KEDACOM TrueVixon100视频会议摄像头
  18. 微信商城小程序WeiMall
  19. 计算机科学与技术行业发展历史,发展历程
  20. JS快速解析Excel文件

热门文章

  1. NumericUpDown
  2. IOS8中SWIFT 弹出框的显示
  3. oracle 使用nfs挂载的目录不能进行归档
  4. 可穿戴医疗设备火爆背后的困境
  5. 简单几行代码,写一个百度广告屏蔽插件,爽到爆
  6. 用MyEclipse开发Spring入门
  7. ospf路由汇总的目的
  8. 机器学习中的模型评价、模型选择及算法选择
  9. node.js是开源的吗_为开源做贡献并不难:我为Node.js项目做贡献的旅程
  10. 帧编码 场编码_去年,我帮助举办了40场编码活动。 这是我学到的。