转自:
http://www.zicheng.net/article/982042.htm
Mongodb除了高性能外,还有完善的API进行各种查询操作,本教程中,我将为你演示一些通用的方法来实现mongodb的查询,包括组合查询,区间查询,like查询,in查询等方法。希望在阅读本文后能举一反三组合成更复杂的查询。
下面直接上代码,每个代码里有详细的注释,有疑问请留言。
001 |
public class MongoDbFind { |
002 |
public static final int port = 27017; |
003 |
public static final String host = "127.0.0.1"; |
009 |
private MongoDatabase getDb() { |
010 |
MongoClient mongo = new MongoClient(host, port); |
011 |
MongoDatabase db = mongo.getDatabase("new_db"); |
019 |
private MongoCollection getCollection() { |
020 |
MongoDatabase db = getDb(); |
021 |
MongoCollection<Document> table = db.getCollection("user"); |
028 |
public void findOne() { |
029 |
MongoCollection table = getCollection(); |
030 |
BasicDBObject dbObject = new BasicDBObject(); |
032 |
dbObject.put("age",-1); |
033 |
FindIterable iterate = table.find().sort(dbObject).limit(1); |
034 |
System.out.println(iterate.iterator().tryNext()); |
039 |
* @param pageIndex 当前页码 |
041 |
private void findPage(int pageIndex) { |
043 |
MongoCollection table = getCollection(); |
045 |
MongoCursor cursor = table.find().limit(pageSize).skip((pageIndex - 1) * pageSize).iterator(); |
046 |
System.out.println("当前页:" + pageIndex); |
047 |
while (cursor.hasNext()) { |
048 |
System.out.println(cursor.next()); |
052 |
public void findPage() { |
060 |
public void comparison() { |
061 |
MongoCollection table = getCollection(); |
062 |
BasicDBObject dbObject = new BasicDBObject(); |
063 |
dbObject.put("age", 20); |
065 |
MongoCursor cursor = table.find(dbObject).iterator(); |
066 |
while (cursor.hasNext()) { |
067 |
System.out.println(cursor.next()); |
074 |
public void findIn() { |
076 |
List<Integer> list = new ArrayList<>(4); |
081 |
MongoCollection table = getCollection(); |
082 |
BasicDBObject dbObject = new BasicDBObject(); |
084 |
dbObject.put("age", new BasicDBObject("$in", list)); |
085 |
MongoCursor cursor = table.find(dbObject).iterator(); |
086 |
while (cursor.hasNext()) { |
087 |
System.out.println(cursor.next()); |
094 |
public void findGtLt() |
096 |
MongoCollection table = getCollection(); |
097 |
BasicDBObject dbObject = new BasicDBObject(); |
099 |
dbObject.put("age", new BasicDBObject("$gt", 15).append("$lt",25)); |
100 |
MongoCursor cursor = table.find(dbObject).iterator(); |
101 |
while (cursor.hasNext()) { |
102 |
System.out.println(cursor.next()); |
111 |
MongoCollection table = getCollection(); |
112 |
BasicDBObject dbObject = new BasicDBObject(); |
113 |
dbObject.put("age", new BasicDBObject("$ne",17)); |
114 |
MongoCursor cursor = table.find(dbObject).iterator(); |
115 |
while (cursor.hasNext()) { |
116 |
System.out.println(cursor.next()); |
123 |
public void findMulti() |
125 |
MongoCollection table = getCollection(); |
127 |
List<BasicDBObject> objects = new ArrayList<BasicDBObject>(); |
128 |
objects.add(new BasicDBObject("age", new BasicDBObject("$ne", 17))); |
129 |
objects.add(new BasicDBObject("name","zicheng-3")); |
130 |
BasicDBObject query=new BasicDBObject(); |
131 |
query.put("$and",objects); |
132 |
MongoCursor cursor = table.find(query).iterator(); |
133 |
while (cursor.hasNext()) { |
134 |
System.out.println(cursor.next()); |
141 |
public void findRegex() |
143 |
MongoCollection table = getCollection(); |
144 |
BasicDBObject regexQuery = new BasicDBObject(); |
145 |
regexQuery.put("name", new BasicDBObject("$regex", "自成.*-[1-8]")); |
148 |
MongoCursor cursor = table.find(regexQuery).iterator(); |
149 |
while (cursor.hasNext()) { |
150 |
System.out.println(cursor.next()); |