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",
"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<=5
{ "_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" } }
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-05/36312.htm
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<=5
{ "_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" } }
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-05/36312.htm
相关推荐
总之,"mongodb-demo示例"是学习和实践Java与MongoDB结合的宝贵资源,它涵盖了从基本的数据库连接、数据操作到复杂查询和高级特性的完整流程。通过深入研究和实践这个示例,开发者可以更好地理解和应用MongoDB,提升...
以上仅是 MongoDB 查询的一些基本操作,实际上,MongoDB 提供了更多高级查询功能,如聚合框架、正则表达式匹配、地理空间查询等。学习并熟练掌握这些查询技巧,对于有效管理和分析 MongoDB 数据库中的数据至关重要。
### MongoDB高级查询知识点详解 #### 一、基本概念回顾 MongoDB 是一款非常流行的文档型数据库,它使用 JSON 格式的文档存储数据。对于开发者来说,掌握 MongoDB 的查询技巧至关重要,尤其是在处理复杂查询需求时。...
### MongoDB查询详解 #### 一、引言 在NoSQL数据库的世界里,MongoDB因其灵活的数据模型、高性能和可扩展性...在未来的学习和实践中,我们还可以进一步探索其他高级查询特性,如聚合框架等,以应对更复杂的业务需求。
#### 第二部分:MongoDB高级实战开发 **第8讲:高级查询** - 正则表达式匹配、文本搜索等高级查询技巧。 - `$lookup`操作符实现多集合联合查询的方法。 - 使用聚合框架进行复杂查询示例。 **第9讲:性能调优** - ...
在实际项目中,你可能还需要处理异常、优化查询性能、使用连接池等高级特性。通过`mongodbdemo`这个示例项目,你可以更深入地了解这些概念和用法。记得在实践中不断探索和学习,以便更好地利用MongoDB为你的应用提供...
`mongodb_learn`这个压缩包可能包含了一些示例代码,用于进一步学习和理解这些高级功能。请参照其中的代码进行学习,并结合官方文档(https://mongodb.github.io/mongo-java-driver/)进行深入研究,以掌握MongoDB ...
Java操作MongoDB是一种常见的数据...实际开发中,可能还需要处理异常、事务管理、分页查询、聚合操作、数据备份和恢复等更高级的主题。了解这些知识点可以帮助开发者有效地利用MongoDB的特性,构建高效的数据处理系统。
本文将深入探讨如何在 MongoDB 中为数组字段和子文档字段创建高级索引,以优化查询性能。 首先,让我们了解一下在数组字段上创建索引的方法。在上述示例中,`tags` 字段是一个数组,包含字符串如 "music", "cricket...
总的来说,这个源码包提供了C#操作MongoDB的实用示例,涵盖了数据库连接、基本操作、高级查询以及聚合操作等方面,对于学习和开发C#与MongoDB集成的应用程序非常有帮助。通过深入理解和实践这些代码,你可以更好地...
在学习过程中,通过阅读中文文档和运行示例,可以加深对MongoDB的理解,掌握其基本操作和高级特性。对于初学者,建议从安装开始,逐步学习数据插入、查询、更新和删除,然后探索索引、聚合、事务等进阶主题。随着对...
理解如何配置和管理分片集群是MongoDB高级主题。 6. 高可用性:MongoDB的复制集(Replica Set)机制可以确保数据的冗余和故障转移,提高服务的连续性。学习如何设置和管理复制集至关重要。 7. 聚合框架:MongoDB的...
- **Recommended Production Architectures**:提供了一些生产环境下的最佳架构示例,帮助开发者构建高效且可扩展的 MongoDB 部署方案。 #### 五、社区与支持 - **Community Info**:这部分包含了 MongoDB 社区的...
4. **高级特性**:深入探讨MongoDB的高级特性,如地理位置索引、TTL索引、文本搜索、网格FS(用于存储大型文件)以及地理空间操作。 5. **应用程序开发**:提供多种编程语言(如Python、Java、Node.js等)的驱动...
Databazel可能是一个用于MongoDB数据分析的工具或框架,虽然具体功能未知,但通常这样的工具会提供更高级的查询和分析功能,简化报表生成过程,或者提供可视化界面来交互式地探索数据。例如,它可以包含数据预处理...
#### 四、高级查询示例 1. **基本查询** - MongoDB支持丰富的查询语言,可用于检索、排序和过滤数据。 - 示例查询语句:`db.collection.find({ field: "value" })` 2. **聚合框架** - MongoDB的聚合框架允许对...
6. NoSQL查询:除了关系型数据库,还有非关系型数据库(NoSQL),它们使用不同的查询方式,例如MongoDB使用MongoDB查询语言(MQL),Cassandra使用CQL(Cassandra Query Language)等。 7. 数据库设计:在进行...