最近在学习MongoDB的JAVA API,便于以后查阅,总结java中实现MongoDB查询的实现方式:
总的来讲,java api中的BasicDBObject相当于mongo命令中的大括号"{}",BasicDBList相当于中括号"[]",这样就比较容易理解下面的语句了。
1.创建连接
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;
// To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
// if it's a member of a replica set:
MongoClient mongoClient = new MongoClient();
// or
MongoClient mongoClient = new MongoClient( "localhost" );
// or
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
new ServerAddress("localhost", 27018),
new ServerAddress("localhost", 27019)));
DB db = mongoClient.getDB( "mydb" );
以上代码引用于MongoDB官网:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started-with-java-driver
后续的db对象参照上面的代码获取。
2. 获取全部collections的名称
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
3. 获取一个collection对象
DBCollection coll = db.getCollection("testCollection");
4. 插入一条记录(Document)
文档内容:
{
"name" : "MongoDB",
"type" : "database",
"count" : 1,
"info" : {
x : 203,
y : 102
}
}
java插入语句:
BasicDBObject doc = new BasicDBObject("name", "MongoDB").
append("type", "database").
append("count", 1).
append("info", new BasicDBObject("x", 203).append("y", 102));
coll.insert(doc);
5. 插入多条记录
for (int i=0; i < 100; i++) {
coll.insert(new BasicDBObject("i", i));
}
6. 进行count操作
System.out.println(coll.getCount());
7. 查询collection中的第一条记录:
mongo语句:
db.mycollection.findOne()
或者
db.mycollection.findOne({})
java语句:
DBCollection coll = db.getCollection("testCollection");
DBObject myDoc = coll.findOne();
System.out.println(myDoc);
8. 使用游标(cursor)遍历记录
DBCursor cursor = coll.find();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
9. 根据条件进行简单查询
java语句:
BasicDBObject queryObj = new BasicDBObject("name", "MongoDB");
cursor = coll.find(query);
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
10. 根据条件进行复杂查询
(1)数值比较$gt(大于)、$lt(小于)、$gte(大于等于)、$lte(小于等于):
Mongo语句:
db.coll.find({"age", {$gt, 10}})
java语句:
BasicDBObject queryObj = new BasicDBObject("age", new BasicDBObject("$gt", 10));
DBCursor cursor = coll.find(queryObj);
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
这里的BasicDBObject相当于mongo语句中的大括号"{ }"
(2)$or操作:
Mongo语句:
db.coll.find({$or, [{"age", 10}, {"age", 15}]})
Java语句:
BasicDBList options = new BasicDBList();
options.add(new BasicDBObject("age", 10));
options.add(new BasicDBObject("age", 15));
DBObject queryObj = new BasicDBObject("$or", options);
DBCursor cursor = coll.find(queryObj);
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
这里的BasicDBList相当于mongo语句中的中括号"[ ]"。
简单总结到这里,如有错误,欢迎批评指正~