/** * MongoDBTest * MongoDB JAVA API的高级查询示例 */ package com.labci.mongodb.test; import java.net.UnknownHostException; import java.util.Iterator; 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; /** * @author Bill Tu(tujiyue/iwtxokhtd) * May 21, 2011[11:33:34 PM] * */ public class MongoDBAdvancedQuery { private static final String HOST = "192.168.1.86"; private static final int PORT = 27017; private static final String USER = "iwtxokhtd"; private static final String PASSWORD = "123456"; private static final String DB_NAME = "test"; private static final String COLLECTION = "data_test"; private static Mongo conn=null; private static DB myDB=null; private static DBCollection myCollection=null; static{ try { conn=new Mongo(HOST,PORT);//建立数据库连接 myDB=conn.getDB(DB_NAME);//使用test数据库 boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证 if(loginSuccess){ myCollection=myDB.getCollection(COLLECTION); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } /** * 查询数据 * @param collection “表”名 */ private static void queryData(DBCollection collection){ //count查询 System.out.println("表的总记录数为:"); System.out.println(collection.find().count()); //分页查询 DBCursor findAll=collection.find(); DBCursor queryByPage=findAll.skip(3).limit(4); printData("从第3条记录起取4条记录为:",queryByPage); //order by操作 DBObject orderBy = new BasicDBObject(); orderBy.put("userName", -1);//按userName倒序排 DBCursor orderByResult=collection.find().sort(orderBy); printData("所有记录按userName倒序排为:",orderByResult); // "!=" 和 ">" 操作 DBObject notEqual=new BasicDBObject(); notEqual.put("$ne", "Bill Tu10"); DBObject greatThan=new BasicDBObject(); greatThan.put("$gt", 7); DBObject notEqualAndGreatThan=new BasicDBObject(); notEqualAndGreatThan.put("userName", notEqual); notEqualAndGreatThan.put("age", greatThan); DBCursor notEqualAndGreatThanResult=collection.find(notEqualAndGreatThan); printData("userName!='Bill Tu10' and age>7的记录为:",notEqualAndGreatThanResult); // ">=" 和"<="操作 DBObject greatThanEqualAndLessThanEqual=new BasicDBObject(); greatThanEqualAndLessThanEqual.put("$gte", 2); greatThanEqualAndLessThanEqual.put("$lte", 7); DBObject ageCompare=new BasicDBObject(); ageCompare.put("age",greatThanEqualAndLessThanEqual); DBCursor compareResult=collection.find(ageCompare); printData("age>=2 and age<=7的记录为:",compareResult); // all操作 DBObject all=new BasicDBObject(); all.put("$all", new Object[]{7,7}); DBObject rankAll=new BasicDBObject(); rankAll.put("rank", all); DBCursor rankAllResult=collection.find(rankAll); printData("rank in all(7,7)的记录为:",rankAllResult); //not in操作 DBObject notIn=new BasicDBObject(); notIn.put("$nin", new Object[]{2,3}); DBObject ageNotIn=new BasicDBObject(); ageNotIn.put("age", notIn); DBCursor ageNotInResult=collection.find(ageNotIn); printData("age not in (2,3)的记录为:",ageNotInResult); //or操作 DBObject orGreatThan=new BasicDBObject("$gt",3); DBObject orRankAll=new BasicDBObject("$all",new Object[]{1,1}); DBObject ageOrGreatThan=new BasicDBObject(); ageOrGreatThan.put("age", orGreatThan); DBObject rankOrAll=new BasicDBObject(); rankOrAll.put("rank", orRankAll); DBObject orOperation=new BasicDBObject(); orOperation.put("$or", new Object[]{ageOrGreatThan,rankOrAll}); DBCursor orResult=collection.find(orOperation); printData("age>3 or rank in all(1,1)的记录为:",orResult); //not or操作 DBObject notOrOperation=new BasicDBObject(); notOrOperation.put("$nor", new Object[]{ageOrGreatThan,rankOrAll}); DBCursor notOrResult=collection.find(notOrOperation); printData("not(age>3 or rank in all(1,1))的记录为:",notOrResult); //size 操作 DBObject size=new BasicDBObject("$size",3); DBObject rankSize=new BasicDBObject(); rankSize.put("rank", size); DBCursor sizeResult=collection.find(rankSize); printData("rank数组大小为3的记录为:",sizeResult); //exists操作 DBObject exists=new BasicDBObject("$exists",false); DBObject userNameExists=new BasicDBObject(); userNameExists.put("userName", exists); DBCursor userNameExistsResult=collection.find(userNameExists); printData("userName exists false的记录为:",userNameExistsResult); //mod 取模操作 DBObject modArray=new BasicDBObject("$mod",new Object[]{2,0}); DBObject ageMod=new BasicDBObject(); ageMod.put("age", modArray); DBCursor ageModResult=collection.find(ageMod); printData("age%2==0的记录为:",ageModResult); } /** * 打印结果数据 * @param description 结果数据相关描述 * @param recordResult 结果集 */ private static void printData(String description,DBCursor recordResult){ System.out.println(description); for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){ System.out.println(iter.next()); } } /** * @param args */ public static void main(String[] args) { queryData(myCollection); } } 对应的查询结果: 表的总记录数为: 10 从第3条记录起取4条记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} 所有记录按userName倒序排为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} userName!='Bill Tu10' and age>7的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}} age>=2 and age<=7的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} rank in all(7,7)的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} age not in (2,3)的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}} age>3 or rank in all(1,1)的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}} not(age>3 or rank in all(1,1))的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}} rank数组大小为3的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}} userName exists false的记录为: age%2==0的记录为: { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}} { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}