「MongoDB入門」BasicDBObjectの使い方

1.Query.put()
date条件検索
BasicDBObject Query = new BasicDBObject();
Query.put(“time",new BasicDBObject(“$gte", date + " 00:00:00″)
.append(“$lte", date + " 23:59:59″));

or条件検索
BasicDBList values = new BasicDBList();
values.add(new BasicDBObject(“status", new BasicDBObject(“$exists",false)));
values.add(new BasicDBObject(“status", 0));
Query.put(“$or", values);

2.match
DBObject Match = new BasicDBObject(“$match", Query);

3.GroupBy.put()
BasicDBObject GroupBy = new BasicDBObject();
GroupBy.put(“userId", “$userId");
GroupBy.put(“status", “$status");

4.Select.put()
DBObject Select = new BasicDBObject(“_id", GroupBy);
Select.put(“count", new BasicDBObject(“$sum", 1));

5.$group
DBObject Group = new BasicDBObject(“$group", Select);
import com.mongodb.AggregationOutput;
import org.springframework.data.mongodb.core.MongoTemplate;
AggregationOutput Output = mongoTemplate.getCollection(“tablenameAA")
.aggregate(Match, Group);
検索結果:
Iterable<DBObject> map = Output.results();
//map loop
for (DBObject dbObject : map) {
Map<String, Object> resultMap = (Map<String, Object>) dbObject.get(“_id");
Integer userId = Integer.parseInt(CommUtil.toString(ausgMap.get(“userId")).replace(“.0″,""));

//count計算
Integer count = Integer.parseInt(CommUtil.toString(dbObject.get(“count")));

}

DataBase

Posted by arkgame