System.out.println("基础初始化部分开始");
Mongo m = new Mongo("localhost", 27017);
// 选择数据库,如果没有这个数据库的话,会自动建立
DB db = m.getDB("ms_basic");
// 建立一个集合,和数据库一样,如果没有,会自动建立
DBCollection coll = db.getCollection("leeCollection");
DBCursor cur = null;
long startTime = 0;
BasicDBObject query = null;
BasicDBObject field = null;
BasicDBObject sort = null;
List<Object> inList = null;
System.out.println("基础初始化部分完毕");
,,,,
public static void showAllDataFromCursor(DBCursor cur) {
while (cur.hasNext()) {
System.out.println(cur.next());
}
}
普通查询
System.out.println("-----------华丽分隔线---------------");
System.out.println("
SELECT * FROM schedule
WHERE startDate='2010-11-18' and disciplineName='田径'
and phaseName='决赛'");
startTime = System.currentTimeMillis();
query = new BasicDBObject();
query.put("startDate","2010-11-18");
query.put("disciplineName", "田径");
query.put("phaseName","决赛");
cur = coll.find(query);
System.out.println("查询获得的长度"+cur.count());
System.out.println("查询耗时:"+(System.currentTimeMillis()-startTime)+"毫秒.");
showAllDataFromCursor(cur);
System.out.println("-----------华丽分隔线---------------");
System.out.println("SELECT * FROM schedule WHERE rsc='ATM033101'");
startTime = System.currentTimeMillis();
query = new BasicDBObject();
query.put("rsc","ATM033101");
cur = coll.find(query);
System.out.println("查询获得的长度"+cur.count());
System.out.println("查询耗时:"+(System.currentTimeMillis()-startTime)+"毫秒.");
showAllDataFromCursor(cur);
System.out.println("-----------华丽分隔线---------------");
System.out.println("
SELECT * FROM schedule
order by startDate asc,startTime asc limit 2,10");
sort = new BasicDBObject();
sort.put("startDate", 1);
sort.put("startTime", 1);
startTime = System.currentTimeMillis();
cur = coll.find().sort(sort).limit(2).skip(10);
System.out.println("查询获得的长度"+cur.count());
System.out.println("查询耗时:"+(System.currentTimeMillis()-startTime)+"毫秒.");
showAllDataFromCursor(cur);
高级查询
http://www.mongodb.org/display/DOCS/Advanced+Queries
Introduction
Retrieving a Subset of Fields
$slice operator
Conditional Operators : <, <=, >, >=
Conditional Operator : $ne
Conditional Operator : $in
Conditional Operator : $nin
Conditional Operator : $mod
Conditional Operator: $all
Conditional Operator : $size
Conditional Operator: $exists
Conditional Operator: $type
$or
Regular Expressions
Value in an Array
Conditional Operator: $elemMatch
Value in an Embedded Object
Meta operator: $not
Javascript Expressions and $where
sort()
limit()
skip()
snapshot()
count()
group()
Special operators
See Also
这里列举部分示例:
? Regular Expressions
System.out.println("-----------华丽分隔线---------------");
System.out.println("
SELECT * FROM schedule
WHERE rsc like 'ATM%'and length(rsc)=9");
startTime = System.currentTimeMillis();
query = new BasicDBObject();
query.put("rsc",Pattern.compile("ATM[A-Z0-9]{6}", Pattern.CASE_INSENSITIVE));
cur = coll.find(query);
System.out.println("查询获得的长度"+cur.count());
System.out.println("查询耗时:"+(System.currentTimeMillis()-startTime)+"毫秒.");
showAllDataFromCursor(cur);
这里可以理解为:rsc like 'ATM%'and length(rsc)=9
? 选择固定字段
System.out.println("-----------华丽分隔线---------------");
System.out.println("SELECT rsc,startDate,startTime, competitionStatus FROM schedule WHERE competitionStatus=1 order by startDate asc,startTime asc limit 10");
startTime = System.currentTimeMillis();
query = new BasicDBObject();
query.put("competitionStatus",1);
field = new BasicDBObject();
field.put("rsc",true);
field.put("startDate",true);
field.put("startTime",true);
field.put("competitionStatus",true);
cur = coll.find(query,field).sort(sort).limit(10);
System.out.println("查询获得的长度"+cur.count());
System.out.println("查询耗时:"+(System.currentTimeMillis()-startTime)+"毫秒.");
showAllDataFromCursor(cur);
本文转自jooben 51CTO博客,原文链接:http://blog.51cto.com/jooben/350747