一. 常用查询:
1. 查询一条数据:(多用于保存时判断db中是否已有当前数据,这里 is 精确匹配,模糊匹配 使用 regex...)
public PageUrl getByUrl(String url) {
return findOne(new Query(Criteria.where("url").is(url)),PageUrl.class);
}
2. 查询多条数据:linkUrl.id 属于分级查询
public List<PageUrl> getPageUrlsByUrl(int begin, int end,String linkUrlid) {
Query query = new Query();
query.addCriteria(Criteria.where("linkUrl.id").is(linkUrlid));
return find(query.limit(end - begin).skip(begin), PageUrl.class);
}
3. 模糊查询:
public long getProcessLandLogsCount(List<Condition> 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);
}
4. gte: 大于等于,lte小于等于...注意查询的时候各个字段的类型要和mongodb中数据类型一致
public List<ProcessLandLog> getProcessLandLogs(int begin,int end,List<Condition> 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<DpsLand> 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 List<GoodsDetail> getGoodsDetails2(int begin, int end) {
Query query = new Query();
query.addCriteria(Criteria.where("goodsSummary").not());
return find(query.limit(end - begin).skip(begin),GoodsDetail.class);
}
5. 查询数量:
public long getPageInfosCount(List<Condition> 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);
}
6. 更新一条数据的一个字段:
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);
}
7. 更新一条数据的多个字段:
//调用更新
private void updateProcessLandLog(ProcessLandLog processLandLog,
int crawlResult) {
List<String> fields = new ArrayList<String>();
List<Object> values = new ArrayList<Object>();
fields.add("state");
fields.add("result");
fields.add("time");
values.add("1");
values.add(crawlResult);
values.add(Calendar.getInstance().getTime());
processLandLogReposity.updateProcessLandLog(processLandLog, fields,
values);
}
//更新
public void updateProcessLandLog(ProcessLandLog land, List<String> fields,List<Object> values) {
Update update = new Update();
int size = fields.size();
for(int i = 0 ; i < size; i++){
String field = fields.get(i);
Object value = values.get(i);
update.set(field, value);
}
updateFirst(new Query(Criteria.where("id").is(land.getId())), update,ProcessLandLog.class);
}
8. 删除数据:
public void deleteObject(Class<T> clazz,String id) {
remove(new Query(Criteria.where("id").is(id)),clazz);
}
9.保存数据:
//插入一条数据
public void saveObject(Object obj) {
insert(obj);
}
//插入多条数据
public void saveObjects(List<T> objects) {
for(T t:objects){
insert(t);
}
}