lyf666 2017-02-20 15:25:47 +08:00
parent 096d98bee7
commit a3cef735ae
2 changed files with 87 additions and 8 deletions

View File

@ -4,11 +4,10 @@ import cn.cloudowr.sdk.FastJSONUtil;
import cn.cloudowr.sdk.jfinal.IdInterceptor; import cn.cloudowr.sdk.jfinal.IdInterceptor;
import cn.cloudowr.sdk.jfinal.RestfulStyle; import cn.cloudowr.sdk.jfinal.RestfulStyle;
import com.jfinal.aop.Before; import com.jfinal.aop.Before;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters; import com.mongodb.client.model.*;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
import org.bson.Document; import org.bson.Document;
@ -18,6 +17,7 @@ import org.bson.types.ObjectId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
/** /**
* Created by lyf66 on 2017/2/16. * Created by lyf66 on 2017/2/16.
@ -31,22 +31,96 @@ public class Controller extends cn.cloudowr.sdk.jfinal.Controller implements Res
String json = getPara("json"); String json = getPara("json");
Document document = null; Document document = null;
if (json != null) { if (json != null) {
document = Document.parse(json); try {
document = Document.parse(json);
} catch (Exception e) {
renderError(400, "invalid json");
return;
}
}
String projStr = getPara("proj");
Document projDoc = null;
if (projStr != null) {
try {
projDoc = Document.parse(projStr);
} catch (Exception e) {
renderError(400, "invalid proj json");
return;
}
}
String sortStr = getPara("sort");
Document sortDoc = null;
if (sortStr != null) {
try {
sortDoc = Document.parse(sortStr);
} catch (Exception e) {
renderError(400, "invalid sort json");
return;
}
} }
List<Bson> filter = new ArrayList<>(); List<Bson> filter = new ArrayList<>();
if (document != null) { if (document != null) {
document.forEach((key, value) -> { document.forEach((key, value) -> {
filter.add(Filters.eq(key, value)); if ("$ex".equals(value)) {
filter.add(Filters.exists(key));
} else if ("$nex" .equals(value)) {
filter.add(Filters.exists(key, false));
}
else if (value != null && value.toString().startsWith("$re")) {
String s = value.toString();
filter.add(Filters.regex(key, Pattern.compile(s.substring(3, s.length()))));
} else {
filter.add(Filters.eq(key, value));
}
}); });
} }
List<Document> result = new ArrayList<>(); List<Document> documents = new ArrayList<>();
MongoCollection<Document> collection = getCollection();
long total;
FindIterable<Document> findIterable;
if (filter.size() != 0) { if (filter.size() != 0) {
getCollection().find(Filters.and(filter)).skip(pageSize * (pageNum - 1)).limit(pageSize).into(result); total = collection.count(Filters.and(filter));
findIterable = collection.find(Filters.and(filter));
} else { } else {
getCollection().find().skip(pageSize * (pageNum - 1)).limit(pageSize).into(result); total = collection.count();
findIterable = collection.find();
} }
int mod = (int) (total % pageSize);
long totalPage = mod == 0 ? total / pageSize : total / pageSize + 1;
findIterable.skip(pageSize * (pageNum - 1)).limit(pageSize);
if (projDoc != null) {
List<Bson> projs = new ArrayList<>();
projDoc.forEach((key, val) -> {
if ("id".equals(key)) {
projs.add(Projections.excludeId());
} else if (1 == (int) val) {
projs.add(Projections.include(key));
} else if (0 == (int) val) {
projs.add(Projections.exclude(key));
}
});
findIterable.projection(Projections.fields(projs));
}
if (sortDoc != null) {
List<Bson> sorts = new ArrayList<>();
sortDoc.forEach((key, val) -> {
if ("id".equals(key)) key = "_id";
if ("asc".equals(val)) {
sorts.add(Sorts.ascending(key));
} else if ("desc".equals(val)) {
sorts.add(Sorts.descending(key));
}
});
findIterable.sort(Sorts.orderBy(sorts));
}
findIterable.into(documents);
Document result = new Document();
result.put("documents", documents);
result.put("total", total);
result.put("totalPage", totalPage);
renderJson(result); renderJson(result);
} }

View File

@ -3,6 +3,11 @@ index (
pageNum optional default 1, pageNum optional default 1,
json optional json optional
) { ) {
special json: {
key: $ex //exists
key: $nex //not exists
key: $revalue //模糊查询,例:$re^te te开头的value
}
return dict list json return dict list json
} }
show (id required) { show (id required) {