1.首先看一下MongoRespository接口的API
2.自定义一个实体类
public class LbsLocation implements Serializable {
private String cityId;
/**
* ACC状态 0:关闭 1:开启
*/
private Integer acc;
@Id
private String id;
/**
* 角度
*
*/
private Integer angle;
/**
* 高度
*
*/
private Integer height;
/**
* 剩余电量
*
*/
private Integer electricity;
/**
* 终端号
*/
private String terminalId;
/**
* 车辆ID
*/
@Indexed
private String vehicleId;
/**
* 速度
*
*/
private Integer speed;
/**
* 状态 0 正常 1:离线 2:异常
*/
private Integer status;
/**
*油门状态 0:正常 1:断油
*/
private Integer oilState;
/**
* 电状态 0:正常 1:断电
*
*/
private Integer powerState;
/**
* 锁状态 0:正常 1:锁车
*
*/
private Integer lockState;
/**
* 上报模式
*/
private Integer reportMod;
/**
* 上报间隔
*/
private Long reportInterval;
/**
* 位置 [lng(经度),lat(纬度)]
*/
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private Double[] location;
/**
* 更新时间
*
*/
@Indexed
private Date time;
private String imei;
private String iccid;
}
3.自定义一个仓库接口
public interface LocationDao extends MongoRepository<LbsLocation,String> {
List<LbsLocation> findByLocationNear(Point point, Distance min, Distance max);
List<LbsLocation> findByLocationNear(Point point, Distance max);
List<LbsLocation> findByLocationWithin(Polygon polygon);
}
- 根据实体类中的属性进行查询:
- 当需要根据实体类中的属性查询时,MongoRepository提供的方法已经不能满足,我们需要在PersonRepository仓库中定义方法,定义方法名的规则为:find + By + 属性名(首字母大写),
- 根据实体类中的属性进行模糊查询:
- 当需要根据实体类中的属性进行模糊查询时,我们也需要在PersonRepository仓库中定义方法,模糊查询定义方法名的规则为:find + By + 属性名(首字母大写) + Like,如:根据姓名进行模糊查询Person
- 带分页的模糊查询,其实是把模糊查询以及分页进行合并,同时我们也需要在PersonRepository仓库中定义方法,定义方法名的规则和模糊查询的规则一致,只是参数不同而已。
GreaterThan(大于)
方法名举例:
findByCityIdGreaterThan(int cityId)
LessThan(小于)
方法名举例:
findByCityIdLessThan(int cityId)
Between(在…之间)
方法名举例:
findByCityIdBetween(int from, int to)
Not(不包含)
方法名举例:
findByVehicleIdNot(String vehicleId)
Near(查询地理位置相近的)
方法名举例:
List<LbsLocation> findByLocationNear(Point point, Distance max);