• spring-data-mongodb必须了解的操作


    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 使用笔记 - yourwafer - 巍巍的博客
  • 相关阅读:
    JVM源码分析之Object.wait/notify(All)完全解读
    进程无故消失的破案历程
    Jmeter——JDBC Connection Configuration参数化
    Jmeter——CSV DataSet Config参数化
    WeTest明星工具-移动端性能测试PerfDog初探
    基于appium实现的线性代码引用unittest单元测试框架
    Requests实践详解
    Appium-Server与Appium-Desktop的区别
    Appium Python API 中文版
    单元测试框架Uinttest一文详解
  • 原文地址:https://www.cnblogs.com/fx2008/p/3582378.html
Copyright © 2020-2023  润新知