Mongodb高级查询
public class MongoDbFind {
002 public static final int port = 27017;
003 public static final String host = "127.0.0.1";
004 /**
005 * 获取数据库对象
006 *
007 * @return
008 */
009 private MongoDatabase getDb() {
010 MongoClient mongo = new MongoClient(host, port);
011 MongoDatabase db = mongo.getDatabase("new_db");
012 return db;
013 }
014 /**
015 * 获取表(集合)
016 *
017 * @return
018 */
019 private MongoCollection getCollection() {
020 MongoDatabase db = getDb();
021 MongoCollection<Document> table = db.getCollection("user");
022 return table;
023 }
024 /**
025 * 查询出一条数据
026 */
027 @Test
028 public void findOne() {
029 MongoCollection table = getCollection();
030 BasicDBObject dbObject = new BasicDBObject();
031 //mongodb中按age字段倒序查询(-1是倒序,1是正序)
032 dbObject.put("age",-1);
033 FindIterable iterate = table.find().sort(dbObject).limit(1);
034 System.out.println(iterate.iterator().tryNext());
035 }
036 /**
037 * mongodb分页查询
038 *
039 * @param pageIndex 当前页码
040 */
041 private void findPage(int pageIndex) {
042 int pageSize = 3;
043 MongoCollection table = getCollection();
044 //mongodb分页查询出游标
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());
049 }
050 }
051 @Test
052 public void findPage() {
053 findPage(1);
054 findPage(2);
055 }
056 /**
057 * 等于查询
058 */
059 @Test
060 public void comparison() {
061 MongoCollection table = getCollection();
062 BasicDBObject dbObject = new BasicDBObject();
063 dbObject.put("age", 20);
064 //查询出年龄是20的记录
065 MongoCursor cursor = table.find(dbObject).iterator();
066 while (cursor.hasNext()) {
067 System.out.println(cursor.next());
068 }
069 }
070 /**
071 * 使用in包含查询
072 */
073 @Test
074 public void findIn() {
075 //定义一个数组存储in查询的条件值
076 List<Integer> list = new ArrayList<>(4);
077 list.add(20);
078 list.add(28);
079 list.add(15);
080 list.add(13);
081 MongoCollection table = getCollection();
082 BasicDBObject dbObject = new BasicDBObject();
083 //这里使用BasicDBObject嵌套来并使用$in预定义名称来实现mongodb的in查询功能
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());
088 }
089 }
090 /**
091 * 大于小于区间查询
092 */
093 @Test
094 public void findGtLt()
095 {
096 MongoCollection table = getCollection();
097 BasicDBObject dbObject = new BasicDBObject();
098 //mongodb实现大于小于区间的查询,注意大于是$gt,小于是$lt
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());
103 }
104 }
105 /**
106 * 不等于查询
107 */
108 @Test
109 public void findNe()
110 {
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());
117 }
118 }
119 /**
120 * 多条件查询
121 */
122 @Test
123 public void findMulti()
124 {
125 MongoCollection table = getCollection();
126 //多条件查询需要先定义一个BasicDBObject数组来存储多个条件
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());
135 }
136 }
137 /**
138 * 通过使用正则表达式模糊查询
139 */
140 @Test
141 public void findRegex()
142 {
143 MongoCollection table = getCollection();
144 BasicDBObject regexQuery = new BasicDBObject();
145 regexQuery.put("name", new BasicDBObject("$regex", "自成.*-[1-8]"));
146 //打印mongodb的查询条件
147 // System.out.println(regexQuery.toString());
148 MongoCursor cursor = table.find(regexQuery).iterator();
149 while (cursor.hasNext()) {
150 System.out.println(cursor.next());
151 }
152 }
153 }