References
- Query, Update, and Projection Operators Quick Reference
原文出自:http://www.mkyong.com/mongodb/java-mongodb-query-document/
返回目录:http://ysj5125094.iteye.com/blog/2192754
In this tutorial, we show you few common ways to get or query document from collection.
译:在本教程中,我们向你展示一些常见的从集合获取和查询文档的方式。
Insert 5 dummy documents for testing.
译:插入5条虚拟的测试记录(文档)。
{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"} { "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"} { "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"} { "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"} { "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}
1.1 Get first matched document only.
译:获取第一个匹配的文档。
DBObject doc = collection.findOne(); System.out.println(dbObject);
Output
{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"}
1.2 Get all matched documents.
译:获取所有匹配的文档。
DBCursor cursor = collection.find(); while(cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"} { "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"} { "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"} { "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"} { "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}
1.3 Get single field from matched document.
译:从匹配的文档中获取单个字段。
BasicDBObject allQuery = new BasicDBObject(); BasicDBObject fields = new BasicDBObject(); fields.put("name", 1); DBCursor cursor = collection.find(allQuery, fields); while (cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "_id" : { "$oid" : "id"} , "name" : "mkyong-1"} { "_id" : { "$oid" : "id"} , "name" : "mkyong-2"} { "_id" : { "$oid" : "id"} , "name" : "mkyong-3"} { "_id" : { "$oid" : "id"} , "name" : "mkyong-4"} { "_id" : { "$oid" : "id"} , "name" : "mkyong-5"}
2.1 Get all documents where number = 5
.
译:获取所有number=5的文档。
BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("number", 5); DBCursor cursor = collection.find(whereQuery); while(cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}
2.2 $in
example – Get documents where number in 2, 4 and 5
.
译:$in例子 ---- 获取 number in (2,3,5) 的文档。
BasicDBObject inQuery = new BasicDBObject(); Listlist = new ArrayList (); list.add(2); list.add(4); list.add(5); inQuery.put("number", new BasicDBObject("$in", list)); DBCursor cursor = collection.find(inQuery); while(cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"} { "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"} { "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}
2.3 $gt $lt
example – Get documents where 5 > number > 2
.
译:$gt $lt例子 ---- 获取 5 > number > 2 的文档。
BasicDBObject gtQuery = new BasicDBObject(); gtQuery.put("number", new BasicDBObject("$gt", 2).append("$lt", 5)); DBCursor cursor = collection.find(gtQuery); while(cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"} { "_id" : { "$oid" : "id"} , "number" : 4 , "name" : "mkyong-4"}
2.4 $ne
example – Get documents where number != 4
.
译:$ne例子 ---- 获取 number != 4的文档。
BasicDBObject neQuery = new BasicDBObject(); neQuery.put("number", new BasicDBObject("$ne", 4)); DBCursor cursor = collection.find(neQuery); while(cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"} { "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"} { "_id" : { "$oid" : "id"} , "number" : 3 , "name" : "mkyong-3"} { "_id" : { "$oid" : "id"} , "number" : 5 , "name" : "mkyong-5"}
3.1 $and
example – get documents where number = 2 and name = 'mkyong-2'
.
译:$and
例子 ---- 获取 number = 2 and name = 'mkyong-2' 的文档。
BasicDBObject andQuery = new BasicDBObject(); Listobj = new ArrayList (); obj.add(new BasicDBObject("number", 2)); obj.add(new BasicDBObject("name", "mkyong-2")); andQuery.put("$and", obj); System.out.println(andQuery.toString()); DBCursor cursor = collection.find(andQuery); while (cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "$and" : [ { "number" : 2} , { "name" : "mkyong-2"}]} { "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"}
Find document with regular expression pattern.
译:查询与正则表达式匹配的文档。
4.1 $regex
example – get documents where name like pattern 'Mky.*-[1-3]', case insensitive
.
译:$regex
例子 ---- 获取name like pattern 'Mky.*-[1-3]'的文档,不区分大小写。
BasicDBObject regexQuery = new BasicDBObject(); regexQuery.put("name", new BasicDBObject("$regex", "Mky.*-[1-3]") .append("$options", "i")); System.out.println(regexQuery.toString()); DBCursor cursor = collection.find(regexQuery); while (cursor.hasNext()) { System.out.println(cursor.next()); }
Output
{ "name" : { "$regex" : "Mky.*-[1-3]" , "$options" : "i"}} { "_id" : { "$oid" : "515ad59e3004c89329c7b259"} , "number" : 1 , "name" : "mkyong-1"} { "_id" : { "$oid" : "515ad59e3004c89329c7b25a"} , "number" : 2 , "name" : "mkyong-2"} { "_id" : { "$oid" : "515ad59e3004c89329c7b25b"} , "number" : 3 , "name" : "mkyong-3"}
There are more…
Read this MongoDB operator documentation for complete set of query operators supported in MongoDB.
译:阅读 MongoDB操作文档,了解更多。
package com.mkyong.core; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * Java MongoDB : Query document * * @author mkyong * */ public class QueryApp { public static void insertDummyDocuments(DBCollection collection) { Listlist = new ArrayList (); Calendar cal = Calendar.getInstance(); for (int i = 1; i <= 5; i++) { BasicDBObject data = new BasicDBObject(); data.append("number", i); data.append("name", "mkyong-" + i); // data.append("date", cal.getTime()); // +1 day cal.add(Calendar.DATE, 1); list.add(data); } collection.insert(list); } public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yourdb"); // get a single collection DBCollection collection = db.getCollection("dummyColl"); insertDummyDocuments(collection); System.out.println("1. Find first matched document"); DBObject dbObject = collection.findOne(); System.out.println(dbObject); System.out.println("\n1. Find all matched documents"); DBCursor cursor = collection.find(); while (cursor.hasNext()) { System.out.println(cursor.next()); } System.out.println("\n1. Get 'name' field only"); BasicDBObject allQuery = new BasicDBObject(); BasicDBObject fields = new BasicDBObject(); fields.put("name", 1); DBCursor cursor2 = collection.find(allQuery, fields); while (cursor2.hasNext()) { System.out.println(cursor2.next()); } System.out.println("\n2. Find where number = 5"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("number", 5); DBCursor cursor3 = collection.find(whereQuery); while (cursor3.hasNext()) { System.out.println(cursor3.next()); } System.out.println("\n2. Find where number in 2,4 and 5"); BasicDBObject inQuery = new BasicDBObject(); List list = new ArrayList (); list.add(2); list.add(4); list.add(5); inQuery.put("number", new BasicDBObject("$in", list)); DBCursor cursor4 = collection.find(inQuery); while (cursor4.hasNext()) { System.out.println(cursor4.next()); } System.out.println("\n2. Find where 5 > number > 2"); BasicDBObject gtQuery = new BasicDBObject(); gtQuery.put("number", new BasicDBObject("$gt", 2).append("$lt", 5)); DBCursor cursor5 = collection.find(gtQuery); while (cursor5.hasNext()) { System.out.println(cursor5.next()); } System.out.println("\n2. Find where number != 4"); BasicDBObject neQuery = new BasicDBObject(); neQuery.put("number", new BasicDBObject("$ne", 4)); DBCursor cursor6 = collection.find(neQuery); while (cursor6.hasNext()) { System.out.println(cursor6.next()); } System.out.println("\n3. Find when number = 2 and name = 'mkyong-2' example"); BasicDBObject andQuery = new BasicDBObject(); List obj = new ArrayList (); obj.add(new BasicDBObject("number", 2)); obj.add(new BasicDBObject("name", "mkyong-2")); andQuery.put("$and", obj); System.out.println(andQuery.toString()); DBCursor cursor7 = collection.find(andQuery); while (cursor7.hasNext()) { System.out.println(cursor7.next()); } System.out.println("\n4. Find where name = 'Mky.*-[1-3]', case sensitive example"); BasicDBObject regexQuery = new BasicDBObject(); regexQuery.put("name", new BasicDBObject("$regex", "Mky.*-[1-3]") .append("$options", "i")); System.out.println(regexQuery.toString()); DBCursor cursor8 = collection.find(regexQuery); while (cursor8.hasNext()) { System.out.println(cursor8.next()); } collection.drop(); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }