博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-data-mongodb必须了解的操作
阅读量:6982 次
发布时间:2019-06-27

本文共 3298 字,大约阅读时间需要 10 分钟。

 在线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
@Query("{ 'firstname' : ?0 }") List
findByThePersonsFirstname(String firstname); }
2.2查询部分属性
publicinterfacePersonRepositoryextendsMongoRepository
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List
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
    {
    @Id privateString id; @Indexed(unique =true) privateInteger ssn; @Field("fName") privateString firstName; @Indexed privateString lastName; privateInteger age; @Transient privateInteger accountTotal; @DBRef privateList
    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 使用笔记 - yourwafer - 巍巍的博客

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

你可能感兴趣的文章
java线程(2)--同步和锁
查看>>
Rafy 框架 - 大批量导入实体
查看>>
go1
查看>>
使用 Palette 让你的 UI 色彩与内容更贴合
查看>>
关于ASP.NET"未能映射路径"问题
查看>>
详谈如何定制自己的博客园皮肤
查看>>
iBATIS配置文件的特殊使用方法
查看>>
Python正则表达式指南
查看>>
T-SQL 根据年月日创建DateTime
查看>>
【CSS进阶】CSS 颜色体系详解
查看>>
论:CMMI项目策划方法(PP)
查看>>
高可用高性能分布式文件系统FastDFS实践Java程序
查看>>
【Coursera课程笔记】Web智能和大数据Week3_MapReduce
查看>>
从头写个http client(java)
查看>>
Windows Phone笔记索引(总)
查看>>
1分钟破解3dState '学习版'得一些版权信息。
查看>>
我和linux
查看>>
动态调用webservice
查看>>
Java刷题知识点之方法覆盖(方法重写)和方法重载的区别
查看>>
爆牙齿的世界杯日记(小组首轮)
查看>>