mongodb高级查询实例

  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" } }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值