[Root @ localhost ~] # Export mongodbshellversion: 1.8.1connectingto: testgt; dbtestgt; showcollections
[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) // query by PAGE, starting from 4th records, 4 records per page.
{"_ 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) // same as the previous one. Note the size and start position of this page.
{"_ 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: in descending order of 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}) // query 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}) // query 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: []}) // query rank = all)
{"_ 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]}) // query rank = all (, 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]}) // query 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]}) // query 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]}) // query 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: []}) // query 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: []}) // query 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}) // query records whose rank array size is 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 "}}
Related reading:
Install and start MongoDB in Linux
Advanced query example in MongoDB
MongoDB Java API for insert and single collection basic query examples
Query operations under MongoDB (corresponding to Java API query operations)
,