|
put("pages", 400);
books.insert(book);
book = new BasicDBObject();
book.put("name", "Understanding Axis2");
book.put("pages", 150);
books.insert(book);
String map = "function() { "+
"var category; " +
"if ( this.pages >= 250 ) "+
"category = 'Big Books'; " +
"else " +
"category = 'Small Books'; "+
"emit(category, {name: this.name});}";
String reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(doc) { " +
"sum += 1; "+
"}); " +
"return {books: sum};} ";
MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput out = books.mapReduce(cmd);
for (DBObject o : out.results()) {
System.out.println(o.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void GroupByManyField() throws UnknownHostException{
//此方法没有运行成功
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("libary");
DBCollection books = db.getCollection("books");
BasicDBObject groupKeys = new BasicDBObject();
groupKeys.put("total", new BasicDBObject("$sum","pages"));
BasicDBObject condition = new BasicDBObject();
condition.append("pages", new BasicDBObject().put("$gt", 0));
String reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(doc) { " +
"sum += 1; "+
"}); " +
"return {books: sum};} ";
/**
BasicDBList basicDBList = (BasicDBList)db.getCollection("mongodb中集合编码或者编码")
.group(DBObject key, --分组字段,即group by的字段
DBObject cond, --查询中where条件
DBObject initial, --初始化各字段的值
String reduce, --每个分组都需要执行的Function
String finial --终结Funciton对结果进行最终的处理
*/
DBObject obj = books.group(groupKeys, condition, new BasicDBObject(), reduce);
System.out.println(obj);
AggregationOutput ouput = books.aggregate(new BasicDBObject("$group",groupKeys));
System.out.println(ouput.getCommandResult());
System.out.println(books.find(new BasicDBObject("$group",groupKeys)));
}
/**
* 分页查询
*/
public void pageQuery() {
DBCursor cursor = MongoInit.getColl("wujintao").find().skip(0)
.limit(10);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
/**
* 模糊查询
*/
public void likeQuery() {
Pattern john = Pattern.compile("joh?n");
BasicDBObject query = new BasicDBObject("name", john);
// finds all people with "name" matching /joh?n/i
DBCursor cursor = MongoInit.getColl("wujintao").find(query);
}
/**
* 条件删除
*/
public void delete(String dbCollection,String dataID) {
BasicDBObject query = new BasicDBObject();
query.put("id", dataID);
// 找到并且删除,并返回删除的对象
DBObject removeObj = MongoInit.getColl(dbCollection).findAndRemove(query);
System.out.println(removeObj);
}
/**
* 更新
*/
public void update() {
BasicDBObject query = new BasicDBObject();
query.put("name", "liu");
DBObject stuFound = MongoInit.getColl("wujintao").findOne(query);
stuFound.put("name", stuFound.get("name") + "update_1");
MongoInit.getColl("wujintao").update(query, stuFound);
}
public void testGpsData() {
try {
//13862082455:117.13172:029.77659:131.2580
Date d = new Date();
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String dateNowStr = sdf.format(d);
System. |