在线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 }} |
publicinterfacePersonRepositoryextendsMongoRepository2.2查询部分属性@Query("{ 'firstname' : ?0 }") List findByThePersonsFirstname(String firstname); }
publicinterfacePersonRepositoryextendsMongoRepository3bean的配置属性@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List findByThePersonsFirstname(String firstname); }
-
@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; } }