日期:2014-05-16  浏览次数:20330 次

MongoDB下的高级查询示例
[root@localhost ~]# mongo  
MongoDB shell version: 1.8.1 
connecting to: test  
> db  
test  
> show collections  
data_test   

system.indexes  
system.users  
> db.data_test.find().skip(3).limit(4)//分页查询,从第4条记录起,每页4条。  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",   
"other" : "nothing4" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",   
"other" : "nothing5" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",   
"other" : "nothing6" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",   
"other" : "nothing7" } }  
> db.data_test.find({},{},4,3)//与上相同,注意此页大小和起始位置的位置  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",   
"other" : "nothing4" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",   
"other" : "nothing5" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",   
"other" : "nothing6" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",   
"other" : "nothing7" } }  
> db.data_test.find().sort({"userName":-1})//order by:按userName倒序  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5c"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "interests" : { "game" : "game9", "ball" : "ball9",   
"other" : "nothing9" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5b"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "interests" : { "game" : "game8", "ball" : "ball8",   
"other" : "nothing8" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",   
"other" : "nothing7" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",   
"other" : "nothing6" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",   
"other" : "nothing5" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",   
"other" : "nothing4" } }  
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf56"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "interests" : { "game" : "game3", "ball" : "ball3", &