MongoDB下的高级查询示例
[root@localhost ~]# mongo MongoDB shell version: 1.8.1 connecting to: test gt; db test gt; show collections
[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",
"other" : "nothing3" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf55"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "interests" : { "game" : "game2", "ball" : "ball2",
"other" : "nothing2" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5d"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "interests" : { "game" : "game10", "ball" :
"ball10", "other" : "nothing10" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf54"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "interests" : { "game" : "game1", "ball" : "ball1",
"other" : "nothing1" } }
> db.data_test.find({"userName":{$ne:"Bill Tu10"},"age":{$gt:7}})//查询userName!='Bill Tu10' and age>7
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4128"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "interests" : { "game" : "game8", "ball" :
"ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4129"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "interests" : { "game" : "game9", "ball" :
"ball9", "other" : "nothing9" } }
> db.data_test.find({"age":{$gte:2},"age":{$lte:5}})//查询age>=2 and age{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4121"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "interests" : { "game" : "game1", "ball" :
"ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4122"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "interests" : { "game" : "game2", "ball" :
"ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4123"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "interests" : { "game" : "game3", "ball" :
"ball3", "other" : "nothing3" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4124"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "interests" : { "game" : "game4", "ball" :
"ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4125"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "interests" : { "game" : "game5", "ball" :
"ball5", "other" : "nothing5" } }
> db.data_test.find({"rank":{$all:[7,7]}})//查询rank=all(7,7)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
> db.data_test.find({"rank":{$all:[7,7,7]}})//查询rank=all(7,7,7)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
> db.data_test.find({"userName":{$exists:false}})
> db.data_test.find({"age":{$mod:[2,0]}})//查询age%2==0
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
"game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
> db.data_test.find({"rank":{$in:[3,4]}})//查询rank in(3,4)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
"game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
> db.data_test.find({"age":{$nin:[2,3]}})//查询rank not in(2,3)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
"game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
"game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
"game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
> db.data_test.find({$or:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询age>3 or rank=all(1,1)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
"game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
"game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
"game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
> db.data_test.find({$nor:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询not (age>3 or rank=all(1,1))
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
"game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
"game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
> db.data_test.find({"rank":{$size:3}})//查询rank数组大小为3的记录
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
"game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
"game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
"game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
"game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
"game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
相关阅读:
MongoDB Linux下的安装和启动
MongoDB下的高级查询示例
MongoDB Java API for 插入和单collection基本查询使用示例
MongoDB下的查询操作(与Java API查询操作对应)
,