精确查询;
模糊查询;
分页查询,每页多少:
按某个字段排序(或升或降):
查询数量:
大于,小于,
等于;
且,
或,
某个字段不为空,
某个字段不存在,
查询在某个范围内,
删除等等查询。
public CrawlerSource getSource(Integer sourceId) {
Query query=new Query(Criteria.where("source_id").is(sourceId));
DataSource dataSource = sourceDaoImpl.findOne(query);
return dataSource ;
}
如果多个条件
或者:
public CrawlerSource getSource(Integer sourceId,String person_inCharge) {
Query query=new Query();
Criteria criteria = new Criteria();
criteria.and("source_id").is(sourceId);
criteria.and("person_inCharge").is(person_inCharge);
query.addCriteria(criteria);
DataSource dataSource = sourceDaoImpl.findOne(query);
return dataSource ;
}
public CrawlerSource getSource(Integer sourceId) {
Query query=new Query();
Criteria criteria = new Criteria();
query.addCriteria(Criteria.where("name").regex(".*?" +sourceId.toString()+ ".*"));
query.addCriteria(criteria);
DataSource dataSource = sourceDaoImpl.findOne(query);
return dataSource ;
}
或者
public long getProcessLandLogsCount(List conditions)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).regex(".*?\\" +condition.getValue().toString()+ ".*"));
}
}
return count(query, ProcessLandLog.class);
}
public List getProcessLandLogs(int begin,int end,List conditions,String orderField,Direction direction)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
if(condition.getKey().equals("time")){
query.addCriteria(Criteria.where("time").gte(condition.getValue())); //gte: 大于等于
}else if(condition.getKey().equals("insertTime")){
query.addCriteria(Criteria.where("insertTime").gte(condition.getValue()));
}else{
query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue()));
}
}
}
return find(query.limit(end - begin).skip(begin).with(new Sort(new Sort.Order(direction, orderField))), ProcessLandLog.class);
}
或者:
public List getDpsLandsByTime(int begin, int end, Date beginDate,Date endDate) {
return find(new Query(Criteria.where("updateTime").gte(beginDate).lte(endDate)).limit(end - begin).skip(begin),
DpsLand.class);
}
public CrawlerSource getSource(Integer sourceId,String person_inCharge) {
Query query=new Query();
Criteria criteria = new Criteria();
//查询只显示未删除的和没有删除字段的数据
criteria.orOperator(Criteria.where("deleted").is(false),
Criteria.where("deleted").exists(false));
query.addCriteria(criteria);
DataSource dataSource = sourceDaoImpl.findOne(query);
return dataSource ;
}
public List getDataSource (int begin, int end) {
Query query = new Query();
query.addCriteria(Criteria.where("goodsSummary").not());
return find(query.limit(end - begin).skip(begin),DataSource .class);
}
Criteria.where("key1").ne("").ne(null)
Criteria criteria = new Criteria();
criteria.and("key1").is(false);
criteria.and("key2").is(type);
Query query = new Query(criteria);
long totalCount = this.mongoTemplate.count(query, Xxx.class);
var s = {
key1: value1,
key2: {
keyA: valueA,
keyB: valueB
}
};
@Query("{'key2.keyA':?0}")
List findAllBykeyA(String keyA);
public long getPageInfosCount(List conditions) {
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue()));
}
}
return count(query, PageInfo.class);
}
Criteria criteria = new Criteria();
Object [] o = new Object[]{0, 1, 2}; //包含所有
criteria.and("type").in(o);
Query query = new Query(criteria);
query.with(new Sort(new Sort.Order(Direction.ASC, "type"))).with(new Sort(new Sort.Order(Direction.ASC, "title")));
List list = this.mongoTemplate.find(query, WidgetMonitor.class);
public WriteResult updateTime(PageUrl pageUrl) {
String id = pageUrl.getId();
return updateFirst(new Query(Criteria.where("id").is(id)),Update.update("updateTime", pageUrl.getUpdateTime()), PageUrl.class);
}
//调用更新
private void updateProcessLandLog(ProcessLandLog processLandLog,
int crawlResult) {
List fields = new ArrayList();
List
public void deleteObject(Class clazz,String id) {
remove(new Query(Criteria.where("id").is(id)),clazz);
}
//插入一条数据
public void saveObject(Object obj) {
insert(obj);
}
//插入多条数据
public void saveObjects(List objects) {
for(T t:objects){
insert(t);
}
}
下面例子包含:
精确查询:is;
模糊查询:regex;
分页查询,每页多少:skip,limit
按某个字段排序(或升或降):new Sort(new Sort.Order(Sort.Direction.ASC, "port"))
查询数量:count
public Map getAppPortDetailByPage(int pageNo, int pageSize, String order, String sortBy, String appPortType, String appPortSeacherName) {
Criteria criteria = new Criteria();
if (!appPortType.equals("")) {
if (!appPortType.equals("all")) {
//DB表里的字段----appmanageType
//下同 port protocol 也是DB表的字段
criteria.and("appmanageType").is(appPortType);
}
}
if (!appPortSeacherName.equals("")) {
try {
criteria.orOperator(Criteria.where("port").is(Integer.parseInt(appPortSeacherName)),
Criteria.where("protocol").regex(".*?" + appPortSeacherName + ".*"));
}catch (Exception e){
criteria.orOperator(Criteria.where("protocol").regex(".*?" + appPortSeacherName + ".*"));
}
}
Map result = Maps.newHashMap();
Query query = new Query(criteria);
query.skip((pageNo - 1) * pageSize);
query.limit(pageSize);
if(order != null && sortBy != null){
query.with(new Sort(new Sort.Order(order.equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, sortBy)));
}else {
query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "port")));
}
List list = this.mongoTemplate.find(query, Appportmanage.class);
long count = this.mongoTemplate.count(query, Appportmanage.class);
result.put("datas", list);
result.put("size", count);
return result;
}