MongoDB JAVA API高级应用

来源:互联网 发布:网络票和窗口票比例 编辑:程序博客网 时间:2024/05/21 09:31

1. MongoDB下的高级查询示例

[java] view plaincopyprint?
  1. [root@localhost ~]# mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > db  
  5. test  
  6. > show collections  
  7. data_test  
  8. system.indexes  
  9. system.users  
  10. > db.data_test.find().skip(3).limit(4)//分页查询,从第4条记录起,每页4条。  
  11. "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""interests" : { "game" : "game4""ball" : "ball4",   
  12. "other" : "nothing4" } }  
  13. "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""interests" : { "game" : "game5""ball" : "ball5",   
  14. "other" : "nothing5" } }  
  15. "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""interests" : { "game" : "game6""ball" : "ball6",   
  16. "other" : "nothing6" } }  
  17. "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""interests" : { "game" : "game7""ball" : "ball7",   
  18. "other" : "nothing7" } }  
  19. > db.data_test.find({},{},4,3)//与上相同,注意此页大小和起始位置的位置  
  20. "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""interests" : { "game" : "game4""ball" : "ball4",   
  21. "other" : "nothing4" } }  
  22. "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""interests" : { "game" : "game5""ball" : "ball5",   
  23. "other" : "nothing5" } }  
  24. "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""interests" : { "game" : "game6""ball" : "ball6",   
  25. "other" : "nothing6" } }  
  26. "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""interests" : { "game" : "game7""ball" : "ball7",   
  27. "other" : "nothing7" } }  
  28. > db.data_test.find().sort({"userName":-1})//order by:按userName倒序  
  29. "_id" : ObjectId("4dd7c914b2d5f68db79cdf5c"), "userId" : "10010179""userName" : "Bill Tu9""gender" : "m9""interests" : { "game" : "game9""ball" : "ball9",   
  30. "other" : "nothing9" } }  
  31. "_id" : ObjectId("4dd7c914b2d5f68db79cdf5b"), "userId" : "10010178""userName" : "Bill Tu8""gender" : "m8""interests" : { "game" : "game8""ball" : "ball8",   
  32. "other" : "nothing8" } }  
  33. "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""interests" : { "game" : "game7""ball" : "ball7",   
  34. "other" : "nothing7" } }  
  35. "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""interests" : { "game" : "game6""ball" : "ball6",   
  36. "other" : "nothing6" } }  
  37. "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""interests" : { "game" : "game5""ball" : "ball5",   
  38. "other" : "nothing5" } }  
  39. "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""interests" : { "game" : "game4""ball" : "ball4",   
  40. "other" : "nothing4" } }  
  41. "_id" : ObjectId("4dd7c914b2d5f68db79cdf56"), "userId" : "10010173""userName" : "Bill Tu3""gender" : "m3""interests" : { "game" : "game3""ball" : "ball3",   
  42. "other" : "nothing3" } }  
  43. "_id" : ObjectId("4dd7c914b2d5f68db79cdf55"), "userId" : "10010172""userName" : "Bill Tu2""gender" : "m2""interests" : { "game" : "game2""ball" : "ball2",   
  44. "other" : "nothing2" } }  
  45. "_id" : ObjectId("4dd7c914b2d5f68db79cdf5d"), "userId" : "100101710""userName" : "Bill Tu10""gender" : "m10""interests" : { "game" : "game10""ball" :   
  46. "ball10""other" : "nothing10" } }  
  47. "_id" : ObjectId("4dd7c914b2d5f68db79cdf54"), "userId" : "10010171""userName" : "Bill Tu1""gender" : "m1""interests" : { "game" : "game1""ball" : "ball1",   
  48. "other" : "nothing1" } }  
  49. > db.data_test.find({"userName":{$ne:"Bill Tu10"},"age":{$gt:7}})//查询userName!='Bill Tu10' and age>7  
  50. "_id" : ObjectId("4dd7cf07b2d5f535b69b4128"), "userId" : "10010178""userName" : "Bill Tu8""gender" : "m8""age" : 8"interests" : { "game" : "game8""ball" :   
  51. "ball8""other" : "nothing8" } }  
  52. "_id" : ObjectId("4dd7cf07b2d5f535b69b4129"), "userId" : "10010179""userName" : "Bill Tu9""gender" : "m9""age" : 9"interests" : { "game" : "game9""ball" :   
  53. "ball9""other" : "nothing9" } }  
  54. > db.data_test.find({"age":{$gte:2},"age":{$lte:5}})//查询age>=2 and age<=5  
  55. "_id" : ObjectId("4dd7cf07b2d5f535b69b4121"), "userId" : "10010171""userName" : "Bill Tu1""gender" : "m1""age" : 1"interests" : { "game" : "game1""ball" :   
  56. "ball1""other" : "nothing1" } }  
  57. "_id" : ObjectId("4dd7cf07b2d5f535b69b4122"), "userId" : "10010172""userName" : "Bill Tu2""gender" : "m2""age" : 2"interests" : { "game" : "game2""ball" :   
  58. "ball2""other" : "nothing2" } }  
  59. "_id" : ObjectId("4dd7cf07b2d5f535b69b4123"), "userId" : "10010173""userName" : "Bill Tu3""gender" : "m3""age" : 3"interests" : { "game" : "game3""ball" :   
  60. "ball3""other" : "nothing3" } }  
  61. "_id" : ObjectId("4dd7cf07b2d5f535b69b4124"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""age" : 4"interests" : { "game" : "game4""ball" :   
  62. "ball4""other" : "nothing4" } }  
  63. "_id" : ObjectId("4dd7cf07b2d5f535b69b4125"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""age" : 5"interests" : { "game" : "game5""ball" :   
  64. "ball5""other" : "nothing5" } }  
  65. > db.data_test.find({"rank":{$all:[7,7]}})//查询rank=all(7,7)  
  66. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""age" : 7"rank" : [ 777 ], "interests" : {   
  67. "game" : "game7""ball" : "ball7""other" : "nothing7" } }  
  68. > db.data_test.find({"rank":{$all:[7,7,7]}})//查询rank=all(7,7,7)  
  69. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""age" : 7"rank" : [ 777 ], "interests" : {   
  70. "game" : "game7""ball" : "ball7""other" : "nothing7" } }  
  71. > db.data_test.find({"userName":{$exists:false}})  
  72. > db.data_test.find({"age":{$mod:[2,0]}})//查询age%2==0  
  73. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172""userName" : "Bill Tu2""gender" : "m2""age" : 2"rank" : [ 222 ], "interests" : {   
  74. "game" : "game2""ball" : "ball2""other" : "nothing2" } }  
  75. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""age" : 4"rank" : [ 444 ], "interests" : {   
  76. "game" : "game4""ball" : "ball4""other" : "nothing4" } }  
  77. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""age" : 6"rank" : [ 666 ], "interests" : {   
  78. "game" : "game6""ball" : "ball6""other" : "nothing6" } }  
  79. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178""userName" : "Bill Tu8""gender" : "m8""age" : 8"rank" : [ 888 ], "interests" : {   
  80. "game" : "game8""ball" : "ball8""other" : "nothing8" } }  
  81. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710""userName" : "Bill Tu10""gender" : "m10""age" : 10"rank" : [ 101010 ], "interests" :   
  82. "game" : "game10""ball" : "ball10""other" : "nothing10" } }  
  83. > db.data_test.find({"rank":{$in:[3,4]}})//查询rank in(3,4)  
  84. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173""userName" : "Bill Tu3""gender" : "m3""age" : 3"rank" : [ 333 ], "interests" : {   
  85. "game" : "game3""ball" : "ball3""other" : "nothing3" } }  
  86. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""age" : 4"rank" : [ 444 ], "interests" : {   
  87. "game" : "game4""ball" : "ball4""other" : "nothing4" } }  
  88. > db.data_test.find({"age":{$nin:[2,3]}})//查询rank not in(2,3)  
  89. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171""userName" : "Bill Tu1""gender" : "m1""age" : 1"rank" : [ 111 ], "interests" : {   
  90. "game" : "game1""ball" : "ball1""other" : "nothing1" } }  
  91. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""age" : 4"rank" : [ 444 ], "interests" : {   
  92. "game" : "game4""ball" : "ball4""other" : "nothing4" } }  
  93. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""age" : 5"rank" : [ 555 ], "interests" : {   
  94. "game" : "game5""ball" : "ball5""other" : "nothing5" } }  
  95. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""age" : 6"rank" : [ 666 ], "interests" : {   
  96. "game" : "game6""ball" : "ball6""other" : "nothing6" } }  
  97. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""age" : 7"rank" : [ 777 ], "interests" : {   
  98. "game" : "game7""ball" : "ball7""other" : "nothing7" } }  
  99. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178""userName" : "Bill Tu8""gender" : "m8""age" : 8"rank" : [ 888 ], "interests" : {   
  100. "game" : "game8""ball" : "ball8""other" : "nothing8" } }  
  101. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179""userName" : "Bill Tu9""gender" : "m9""age" : 9"rank" : [ 999 ], "interests" : {   
  102. "game" : "game9""ball" : "ball9""other" : "nothing9" } }  
  103. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710""userName" : "Bill Tu10""gender" : "m10""age" : 10"rank" : [ 101010 ], "interests" :   
  104. "game" : "game10""ball" : "ball10""other" : "nothing10" } }  
  105. > db.data_test.find({$or:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询age>3 or rank=all(1,1)  
  106. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171""userName" : "Bill Tu1""gender" : "m1""age" : 1"rank" : [ 111 ], "interests" : {   
  107. "game" : "game1""ball" : "ball1""other" : "nothing1" } }  
  108. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""age" : 4"rank" : [ 444 ], "interests" : {   
  109. "game" : "game4""ball" : "ball4""other" : "nothing4" } }  
  110. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""age" : 5"rank" : [ 555 ], "interests" : {   
  111. "game" : "game5""ball" : "ball5""other" : "nothing5" } }  
  112. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""age" : 6"rank" : [ 666 ], "interests" : {   
  113. "game" : "game6""ball" : "ball6""other" : "nothing6" } }  
  114. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""age" : 7"rank" : [ 777 ], "interests" : {   
  115. "game" : "game7""ball" : "ball7""other" : "nothing7" } }  
  116. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178""userName" : "Bill Tu8""gender" : "m8""age" : 8"rank" : [ 888 ], "interests" : {   
  117. "game" : "game8""ball" : "ball8""other" : "nothing8" } }  
  118. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179""userName" : "Bill Tu9""gender" : "m9""age" : 9"rank" : [ 999 ], "interests" : {   
  119. "game" : "game9""ball" : "ball9""other" : "nothing9" } }  
  120. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710""userName" : "Bill Tu10""gender" : "m10""age" : 10"rank" : [ 101010 ], "interests" :   
  121. "game" : "game10""ball" : "ball10""other" : "nothing10" } }  
  122. > db.data_test.find({$nor:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询not (age>3 or rank=all(1,1))  
  123. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172""userName" : "Bill Tu2""gender" : "m2""age" : 2"rank" : [ 222 ], "interests" : {   
  124. "game" : "game2""ball" : "ball2""other" : "nothing2" } }  
  125. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173""userName" : "Bill Tu3""gender" : "m3""age" : 3"rank" : [ 333 ], "interests" : {   
  126. "game" : "game3""ball" : "ball3""other" : "nothing3" } }  
  127. > db.data_test.find({"rank":{$size:3}})//查询rank数组大小为3的记录  
  128. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171""userName" : "Bill Tu1""gender" : "m1""age" : 1"rank" : [ 111 ], "interests" : {   
  129. "game" : "game1""ball" : "ball1""other" : "nothing1" } }  
  130. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172""userName" : "Bill Tu2""gender" : "m2""age" : 2"rank" : [ 222 ], "interests" : {   
  131. "game" : "game2""ball" : "ball2""other" : "nothing2" } }  
  132. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173""userName" : "Bill Tu3""gender" : "m3""age" : 3"rank" : [ 333 ], "interests" : {   
  133. "game" : "game3""ball" : "ball3""other" : "nothing3" } }  
  134. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174""userName" : "Bill Tu4""gender" : "m4""age" : 4"rank" : [ 444 ], "interests" : {   
  135. "game" : "game4""ball" : "ball4""other" : "nothing4" } }  
  136. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175""userName" : "Bill Tu5""gender" : "m5""age" : 5"rank" : [ 555 ], "interests" : {   
  137. "game" : "game5""ball" : "ball5""other" : "nothing5" } }  
  138. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176""userName" : "Bill Tu6""gender" : "m6""age" : 6"rank" : [ 666 ], "interests" : {   
  139. "game" : "game6""ball" : "ball6""other" : "nothing6" } }  
  140. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177""userName" : "Bill Tu7""gender" : "m7""age" : 7"rank" : [ 777 ], "interests" : {   
  141. "game" : "game7""ball" : "ball7""other" : "nothing7" } }  
  142. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178""userName" : "Bill Tu8""gender" : "m8""age" : 8"rank" : [ 888 ], "interests" : {   
  143. "game" : "game8""ball" : "ball8""other" : "nothing8" } }  
  144. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179""userName" : "Bill Tu9""gender" : "m9""age" : 9"rank" : [ 999 ], "interests" : {   
  145. "game" : "game9""ball" : "ball9""other" : "nothing9" } }  
  146. "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710""userName" : "Bill Tu10""gender" : "m10""age" : 10"rank" : [ 101010 ], "interests" :   
  147. "game" : "game10""ball" : "ball10""other" : "nothing10" } }  
 
.MongoDB JAVA API高级查询示例

[java] view plaincopyprint?
  1. /** 
  2.  * MongoDBTest 
  3.  * MongoDB JAVA API的高级查询示例 
  4.  */  
  5. package com.labci.mongodb.test;  
  6. import java.net.UnknownHostException;  
  7. import java.util.Iterator;  
  8. import com.mongodb.BasicDBObject;  
  9. import com.mongodb.DB;  
  10. import com.mongodb.DBCollection;  
  11. import com.mongodb.DBCursor;  
  12. import com.mongodb.DBObject;  
  13. import com.mongodb.Mongo;  
  14. import com.mongodb.MongoException;  
  15. /** 
  16.  * @author Bill Tu(tujiyue/iwtxokhtd) 
  17.  * May 21, 2011[11:33:34 PM] 
  18.  * 
  19.  */  
  20. public class MongoDBAdvancedQuery {  
  21.     private static final String HOST = "192.168.1.86";  
  22.     private static final int PORT = 27017;  
  23.     private static final String USER = "iwtxokhtd";  
  24.     private static final String PASSWORD = "123456";  
  25.     private static final String DB_NAME = "test";  
  26.     private static final String COLLECTION = "data_test";  
  27.     private static Mongo conn=null;  
  28.     private static DB myDB=null;  
  29.     private static DBCollection myCollection=null;  
  30.       
  31.     static{  
  32.         try {  
  33.             conn=new Mongo(HOST,PORT);//建立数据库连接  
  34.             myDB=conn.getDB(DB_NAME);//使用test数据库  
  35.             boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证  
  36.             if(loginSuccess){  
  37.                 myCollection=myDB.getCollection(COLLECTION);  
  38.             }  
  39.         } catch (UnknownHostException e) {  
  40.             e.printStackTrace();  
  41.         } catch (MongoException e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45.       
  46.     /** 
  47.      * 查询数据 
  48.      * @param collection “表”名 
  49.      */  
  50.     private static void queryData(DBCollection collection){  
  51.         //count查询  
  52.         System.out.println("表的总记录数为:");  
  53.         System.out.println(collection.find().count());  
  54.         //分页查询  
  55.         DBCursor findAll=collection.find();  
  56.         DBCursor queryByPage=findAll.skip(3).limit(4);  
  57.         printData("从第3条记录起取4条记录为:",queryByPage);  
  58.         //order by操作  
  59.         DBObject orderBy = new BasicDBObject();  
  60.         orderBy.put("userName", -1);//按userName倒序排  
  61.         DBCursor orderByResult=collection.find().sort(orderBy);  
  62.         printData("所有记录按userName倒序排为:",orderByResult);  
  63.           
  64.         // "!=" 和 ">" 操作  
  65.         DBObject notEqual=new BasicDBObject();  
  66.         notEqual.put("$ne""Bill Tu10");  
  67.           
  68.         DBObject greatThan=new BasicDBObject();  
  69.         greatThan.put("$gt"7);  
  70.           
  71.         DBObject notEqualAndGreatThan=new BasicDBObject();  
  72.         notEqualAndGreatThan.put("userName", notEqual);  
  73.         notEqualAndGreatThan.put("age", greatThan);  
  74.           
  75.         DBCursor notEqualAndGreatThanResult=collection.find(notEqualAndGreatThan);  
  76.         printData("userName!='Bill Tu10' and age>7的记录为:",notEqualAndGreatThanResult);  
  77.           
  78.         // ">=" 和"<="操作  
  79.         DBObject greatThanEqualAndLessThanEqual=new BasicDBObject();  
  80.         greatThanEqualAndLessThanEqual.put("$gte"2);  
  81.         greatThanEqualAndLessThanEqual.put("$lte"7);  
  82.           
  83.         DBObject ageCompare=new BasicDBObject();  
  84.         ageCompare.put("age",greatThanEqualAndLessThanEqual);  
  85.           
  86.         DBCursor compareResult=collection.find(ageCompare);  
  87.         printData("age>=2 and age<=7的记录为:",compareResult);  
  88.           
  89.         // all操作  
  90.         DBObject all=new BasicDBObject();  
  91.         all.put("$all"new Object[]{7,7});  
  92.           
  93.         DBObject rankAll=new BasicDBObject();  
  94.         rankAll.put("rank", all);  
  95.           
  96.         DBCursor rankAllResult=collection.find(rankAll);  
  97.         printData("rank in all(7,7)的记录为:",rankAllResult);  
  98.           
  99.         //not in操作  
  100.         DBObject notIn=new BasicDBObject();  
  101.         notIn.put("$nin"new Object[]{2,3});  
  102.           
  103.         DBObject ageNotIn=new BasicDBObject();  
  104.         ageNotIn.put("age", notIn);  
  105.           
  106.         DBCursor ageNotInResult=collection.find(ageNotIn);  
  107.         printData("age not in (2,3)的记录为:",ageNotInResult);  
  108.           
  109.         //or操作  
  110.         DBObject orGreatThan=new BasicDBObject("$gt",3);  
  111.         DBObject orRankAll=new BasicDBObject("$all",new Object[]{1,1});  
  112.         DBObject ageOrGreatThan=new BasicDBObject();  
  113.         ageOrGreatThan.put("age", orGreatThan);  
  114.         DBObject rankOrAll=new BasicDBObject();  
  115.         rankOrAll.put("rank", orRankAll);  
  116.           
  117.         DBObject orOperation=new BasicDBObject();  
  118.         orOperation.put("$or"new Object[]{ageOrGreatThan,rankOrAll});  
  119.           
  120.         DBCursor orResult=collection.find(orOperation);  
  121.         printData("age>3 or rank in all(1,1)的记录为:",orResult);  
  122.           
  123.         //not or操作  
  124.         DBObject notOrOperation=new BasicDBObject();  
  125.         notOrOperation.put("$nor"new Object[]{ageOrGreatThan,rankOrAll});  
  126.           
  127.         DBCursor notOrResult=collection.find(notOrOperation);  
  128.         printData("not(age>3 or rank in all(1,1))的记录为:",notOrResult);  
  129.           
  130.         //size 操作  
  131.         DBObject size=new BasicDBObject("$size",3);  
  132.         DBObject rankSize=new BasicDBObject();  
  133.         rankSize.put("rank", size);  
  134.           
  135.         DBCursor sizeResult=collection.find(rankSize);  
  136.         printData("rank数组大小为3的记录为:",sizeResult);  
  137.           
  138.         //exists操作  
  139.         DBObject exists=new BasicDBObject("$exists",false);  
  140.         DBObject userNameExists=new BasicDBObject();  
  141.         userNameExists.put("userName", exists);  
  142.           
  143.         DBCursor userNameExistsResult=collection.find(userNameExists);  
  144.         printData("userName exists false的记录为:",userNameExistsResult);  
  145.           
  146.         //mod 取模操作  
  147.         DBObject modArray=new BasicDBObject("$mod",new Object[]{2,0});  
  148.         DBObject ageMod=new BasicDBObject();  
  149.         ageMod.put("age", modArray);  
  150.           
  151.         DBCursor ageModResult=collection.find(ageMod);  
  152.         printData("age%2==0的记录为:",ageModResult);  
  153.           
  154.     }  
  155.       
  156.     /** 
  157.      * 打印结果数据 
  158.      * @param description 结果数据相关描述 
  159.      * @param recordResult 结果集 
  160.      */  
  161.     private static void printData(String description,DBCursor recordResult){  
  162.         System.out.println(description);  
  163.         for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){  
  164.             System.out.println(iter.next());  
  165.         }  
  166.     }  
  167.       
  168.     /** 
  169.      * @param args 
  170.      */  
  171.     public static void main(String[] args) {  
  172.         queryData(myCollection);  
  173.     }  
  174. }  
 

对应的查询结果:

[java] view plaincopyprint?
  1. 表的总记录数为:  
  2. 10  
  3. 从第3条记录起取4条记录为:  
  4. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  5. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}  
  6. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  7. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  8. 所有记录按userName倒序排为:  
  9. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}  
  10. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}  
  11. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  12. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  13. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}  
  14. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  15. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}  
  16. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}  
  17. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}  
  18. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}  
  19. userName!='Bill Tu10' and age>7的记录为:  
  20. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}  
  21. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}  
  22. age>=2 and age<=7的记录为:  
  23. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}  
  24. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}  
  25. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  26. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}  
  27. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  28. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  29. rank in all(7,7)的记录为:  
  30. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  31. age not in (2,3)的记录为:  
  32. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}  
  33. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  34. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}  
  35. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  36. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  37. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}  
  38. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}  
  39. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}  
  40. age>3 or rank in all(1,1)的记录为:  
  41. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}  
  42. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  43. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}  
  44. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  45. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  46. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}  
  47. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}  
  48. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}  
  49. not(age>3 or rank in all(1,1))的记录为:  
  50. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}  
  51. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}  
  52. rank数组大小为3的记录为:  
  53. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}  
  54. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}  
  55. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}  
  56. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  57. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}  
  58. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  59. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}  
  60. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}  
  61. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}  
  62. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}  
  63. userName exists false的记录为:  
  64. age%2==0的记录为:  
  65. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}  
  66. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}  
  67. "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}  
  68. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}  
  69. "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}  
 

3.MongoDB JAVA API插入和删除示例

[java] view plaincopyprint?
  1. /** 
  2.  * MongoDBTest 
  3.  * MongoDB插入和删除数据示例 
  4.  */  
  5. package com.labci.mongodb.test;  
  6. import java.net.UnknownHostException;  
  7. import java.util.ArrayList;  
  8. import java.util.Iterator;  
  9. import java.util.List;  
  10. import com.mongodb.BasicDBObject;  
  11. import com.mongodb.DB;  
  12. import com.mongodb.DBCollection;  
  13. import com.mongodb.DBCursor;  
  14. import com.mongodb.DBObject;  
  15. import com.mongodb.Mongo;  
  16. import com.mongodb.MongoException;  
  17. /** 
  18.  *  
  19.  * @author Bill Tu(tujiyue/iwtxokhtd) 
  20.  * May 22, 2011[3:10:56 PM] 
  21.  * 
  22.  */  
  23. public class MongoDBInsertDataTest {  
  24.     private static final String HOST = "192.168.1.86";  
  25.     private static final int PORT = 27017;  
  26.     private static final String USER = "iwtxokhtd";  
  27.     private static final String PASSWORD = "123456";  
  28.     private static final String DB_NAME = "test";  
  29.     private static final String COLLECTION = "insert_test";  
  30.     private static Mongo conn=null;  
  31.     private static DB myDB=null;  
  32.     private static DBCollection myCollection=null;  
  33.       
  34.     static{  
  35.         try {  
  36.             conn=new Mongo(HOST,PORT);//建立数据库连接  
  37.             myDB=conn.getDB(DB_NAME);//使用test数据库  
  38.             boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证  
  39.             if(loginSuccess){  
  40.                 myCollection=myDB.getCollection(COLLECTION);  
  41.             }  
  42.         } catch (UnknownHostException e) {  
  43.             e.printStackTrace();  
  44.         } catch (MongoException e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48.       
  49.     /** 
  50.      * 使用save()方法保存数据 
  51.      * @param collection “表”名 
  52.      */  
  53.     private static void saveData(DBCollection collection){  
  54.         DBObject saveData=new BasicDBObject();  
  55.         saveData.put("userName""iwtxokhtd");  
  56.         saveData.put("age""26");  
  57.         saveData.put("gender""m");  
  58.           
  59.         DBObject infoData=new BasicDBObject();  
  60.         infoData.put("height"16.3);  
  61.         infoData.put("weight"22);  
  62.           
  63.         saveData.put("info", infoData);  
  64.         collection.save(saveData);  
  65.     }  
  66.       
  67.     /** 
  68.      * 使用insert()方法保存数据 
  69.      * @param collection "表"名 
  70.      */  
  71.     private static void insertData(DBCollection collection){  
  72.         DBObject insertData=new BasicDBObject();  
  73.         insertData.put("name""pig");  
  74.         insertData.put("headers"2);  
  75.         insertData.put("legs"4);  
  76.         //insert(DBObject obj)  
  77.         collection.insert(insertData);  
  78.           
  79.         DBObject insertDataFox=new BasicDBObject();  
  80.         insertDataFox.put("name""fox");  
  81.         insertDataFox.put("headers"1);  
  82.         insertDataFox.put("legs"4);  
  83.           
  84.         DBObject insertDataTiger=new BasicDBObject();  
  85.         insertDataTiger.put("name""tiger");  
  86.         insertDataTiger.put("headers"1);  
  87.         insertDataTiger.put("legs"3);  
  88.           
  89.         List<DBObject> insertDataList=new ArrayList<DBObject>();  
  90.         insertDataList.add(insertDataFox);  
  91.         insertDataList.add(insertDataTiger);  
  92.         //insert(List<DBObject> list)  
  93.         collection.insert(insertDataList);  
  94.     }  
  95.       
  96.     /** 
  97.      * 取得查询结果集 
  98.      * @param collection 
  99.      * @return 
  100.      */  
  101.     private static DBCursor getResult(DBCollection collection){  
  102.         return collection.find();  
  103.     }  
  104.       
  105.     /** 
  106.      * 打印结果数据 
  107.      * @param description 结果数据相关描述 
  108.      * @param recordResult 结果集 
  109.      */  
  110.     private static void printData(String description,DBCursor recordResult){  
  111.         System.out.println(description);  
  112.         for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){  
  113.             System.out.println(iter.next());  
  114.         }  
  115.     }  
  116.       
  117.       
  118.     /** 
  119.      * @param args 
  120.      */  
  121.     public static void main(String[] args) {  
  122.         //插入数据前若有记录才先删除所有记录  
  123.         if(myCollection.find().count()>0){  
  124.             myCollection.remove(new BasicDBObject());  
  125.         }  
  126.         //再插入数据  
  127.         saveData(myCollection);  
  128.         insertData(myCollection);  
  129.         //再查询数据  
  130.         printData("插入数据后的所有结果记录为:",getResult(myCollection));  
  131.         //再删除指定的数据  
  132.         DBObject deletePig=new BasicDBObject();  
  133.         deletePig.put("name""pig");  
  134.         myCollection.remove(deletePig);  
  135.         printData("删除pig后的所有结果记录为:",getResult(myCollection));  
  136.           
  137.     }  
  138. }  
 

结果:

[java] view plaincopyprint?
  1. 插入数据后的所有结果记录为:  
  2. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af38"} , "userName" : "iwtxokhtd" , "age" : "26" , "gender" : "m" , "info" : { "height" : 16.3 , "weight" : 22}}  
  3. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af39"} , "name" : "pig" , "headers" : 2 , "legs" : 4}  
  4. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3a"} , "name" : "fox" , "headers" : 1 , "legs" : 4}  
  5. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3b"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}  
  6. 删除pig后的所有结果记录为:  
  7. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af38"} , "userName" : "iwtxokhtd" , "age" : "26" , "gender" : "m" , "info" : { "height" : 16.3 , "weight" : 22}}  
  8. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3a"} , "name" : "fox" , "headers" : 1 , "legs" : 4}  
  9. "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3b"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}  
 

4.MongoDB JAVA API更新数据示例

[java] view plaincopyprint?
  1. /** 
  2.  * MongoDBTest 
  3.  * MongoDB更新数据使用示例 
  4.  */  
  5. package com.labci.mongodb.test;  
  6. import java.net.UnknownHostException;  
  7. import java.util.Iterator;  
  8. import com.mongodb.BasicDBObject;  
  9. import com.mongodb.DB;  
  10. import com.mongodb.DBCollection;  
  11. import com.mongodb.DBCursor;  
  12. import com.mongodb.DBObject;  
  13. import com.mongodb.Mongo;  
  14. import com.mongodb.MongoException;  
  15. /** 
  16.  *  
  17.  * @author Bill Tu(tujiyue/iwtxokhtd) 
  18.  * May 22, 2011[3:10:56 PM] 
  19.  * 
  20.  */  
  21. public class MongoDBUpdateDataTest {  
  22.     private static final String HOST = "192.168.1.86";  
  23.     private static final int PORT = 27017;  
  24.     private static final String USER = "iwtxokhtd";  
  25.     private static final String PASSWORD = "123456";  
  26.     private static final String DB_NAME = "test";  
  27.     private static final String COLLECTION = "insert_test";  
  28.     private static Mongo conn=null;  
  29.     private static DB myDB=null;  
  30.     private static DBCollection myCollection=null;  
  31.       
  32.     static{  
  33.         try {  
  34.             conn=new Mongo(HOST,PORT);//建立数据库连接  
  35.             myDB=conn.getDB(DB_NAME);//使用test数据库  
  36.             boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证  
  37.             if(loginSuccess){  
  38.                 myCollection=myDB.getCollection(COLLECTION);  
  39.             }  
  40.         } catch (UnknownHostException e) {  
  41.             e.printStackTrace();  
  42.         } catch (MongoException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46.       
  47.     /** 
  48.      * 更新“表”数据 
  49.      * @param collection 
  50.      */  
  51.     private static void updateData(DBCollection collection){  
  52.         DBObject updateCondition=new BasicDBObject();  
  53.           
  54.         //where name='fox'  
  55.         updateCondition.put("name""fox");  
  56.           
  57.         DBObject updatedValue=new BasicDBObject();  
  58.         updatedValue.put("headers"3);  
  59.         updatedValue.put("legs"4);  
  60.           
  61.         DBObject updateSetValue=new BasicDBObject("$set",updatedValue);  
  62.         /** 
  63.          * update insert_test set headers=3 and legs=4 where name='fox' 
  64.          * updateCondition:更新条件 
  65.          * updateSetValue:设置的新值 
  66.          */  
  67.         collection.update(updateCondition, updateSetValue);  
  68.           
  69.         DBObject queryCondition=new BasicDBObject();  
  70.           
  71.         //where name='sam',此条件在更新前不是成立的  
  72.         queryCondition.put("name""sam");  
  73.           
  74.         DBObject setValue=new BasicDBObject();  
  75.         setValue.put("headers"1);  
  76.         setValue.put("legs"1);  
  77.           
  78.         DBObject upsertValue=new BasicDBObject("$set",setValue);  
  79.         /** 
  80.          * 后面两个参数含义分别是: 
  81.          * 若所更新的数据没有,则插入 
  82.          * ,同时更新多个符合条件的文档(collection) 
  83.          */  
  84.         collection.update(queryCondition, upsertValue, truetrue);  
  85.         //set headers=headers+2  
  86.         DBObject incValue=new BasicDBObject("headers",2);  
  87.         //set legs=4  
  88.         DBObject legsValue=new BasicDBObject("legs",4);  
  89.           
  90.         DBObject allCondition=new BasicDBObject();  
  91.         allCondition.put("$inc", incValue);  
  92.         allCondition.put("$set", legsValue);  
  93.           
  94.         collection.update(queryCondition, allCondition);  
  95.     }  
  96.       
  97.     /** 
  98.      * 返回查询结果集 
  99.      * @param collection 
  100.      * @return 
  101.      */  
  102.     private static DBCursor queryData(DBCollection collection){  
  103.         DBCursor queriedData=collection.find();  
  104.         return queriedData;  
  105.           
  106.     }  
  107.       
  108.     /** 
  109.      * 打印结果数据 
  110.      * @param description 结果数据相关描述 
  111.      * @param recordResult 结果集 
  112.      */  
  113.     private static void printData(String description,DBCursor recordResult){  
  114.         System.out.println(description);  
  115.         for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){  
  116.             System.out.println(iter.next());  
  117.         }  
  118.     }  
  119.       
  120.       
  121.     /** 
  122.      * @param args 
  123.      */  
  124.     public static void main(String[] args) {  
  125.       
  126.         printData("查看一下更新前的数据:",queryData(myCollection));  
  127.         //更新数据  
  128.         updateData(myCollection);  
  129.         printData("查看一下更新后的数据:",queryData(myCollection));  
  130.           
  131.     }  
  132. }  
 

运行结果:

[java] view plaincopyprint?
  1. 查看一下更新前的数据:  
  2. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}  
  3. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}  
  4. "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 1 , "legs" : 1 , "name" : "sam"}  
  5. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height" : 16.3 , "weight" : 22} , "legs" : 4 , "userName" : "iwtxokhtd"}  
  6. 查看一下更新后的数据:  
  7. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}  
  8. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}  
  9. "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 3 , "legs" : 4 , "name" : "sam"}  
  10. "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height"


本文转载自: 点击打开链接
作者  :tujiyue

0 0
原创粉丝点击