1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use admin 3 > db.grantRolesToUser( "useradmin",[{ role: "dbOwner",db:"mydb" }]) #受權 4 > use mydb #若存在mymongodb則切換,若不存在則建立 5 > show dbs 6 admin 0.000GB 7 config 0.000GB 8 local 0.000GB 9 > db.age01.insert({"name": "xianghongying"}) 10 WriteResult({ "nInserted" : 1 }) #輸出信息 11 > show dbs 12 admin 0.000GB 13 config 0.000GB 14 local 0.000GB 15 mydb 0.000GB
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 switched to db mydb #輸出信息 4 > db.dropDatabase() 5 { "dropped" : "mydb", "ok" : 1 }
1 db.createCollection(name, options)
1 > use mydb 2 > db.createCollection("age01") 3 { "ok" : 1 } #輸出信息 4 > show tables #也可以使用show collections 5 age01 6 > db.createCollection("age02",{ capped: true, autoIndexId : true, size: 6142800, max: 10000}) 7 { 8 "note" : "the autoIndexId option is deprecated and will be removed in a future release", 9 "ok" : 1 10 } 11 #建立固定集合 age02,整個集合空間大小 6142800 KB, 文檔最大個數爲 10000 個。 12 > db.age03.insert({"name": "xianghongying","age": "18"}) 13 WriteResult({ "nInserted" : 1 }) 14 > show tables 15 age01 16 age02 17 age03
1 db.collection.drop()
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > show tables 4 age01 5 age02 6 age03 7 > db.age03.drop() 8 true 9 > show tables 10 age01 11 age02
1 db.COLLECTION_NAME.insert(document) 2 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 3 > use mydb 4 > db.age01.insert({name: 'zhangsan', 5 age: '18', 6 tel: '123456781', 7 love: ['apple','banana'] 8 }) 9 WriteResult({ "nInserted" : 1 }) #輸出信息
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.find() 4 > db.age01.find().pretty()
1 > lisi_age=({name: 'lisi', age: '19', tel: '123456782', love: ['apple','orange']}) 2 > db.age01.insert(lisi_age) 3 WriteResult({ "nInserted" : 1 }) #輸出信息 4 > db.age01.find().pretty()
1 > db.age01.insertOne({name: 'wanger', age: '15', tel: '123456783', love: ['pear','orange']}) 2 向指定集合中插入多條文檔數據。 3 > db.age01.insertMany([{name: 'mazi', age: '15', tel: '123456784', love: ['pear','apple']},{name: 'xiaoming', age: '8', tel: '123456785', love: ['apple','grape']}])
1 > db.age01.find().pretty()
1 db.collection.update( 2 <query>, 3 <update>, 4 { 5 upsert: <boolean>, 6 multi: <boolean>, 7 writeConcern: <document> 8 } 9 )
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.update({name: "xiaoming"},{$set:{love: ['strawberry','orange']}}) 4 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) #輸出信息 5 > db.age01.find().pretty()
1 > db.age01.update({age: '15'},{$set:{tel: '133333333'}},{multi:true}) #修改多條 2 WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) 3 > db.age01.find().pretty()
1 db.collection.save( 2 <document>, 3 { 4 writeConcern: <document> 5 } 6 )
1 > db.age01.save({_id: ObjectId("5ceb9b2785cdc85ba0f3d6a3"),name: 'zhangsan', age: '14', tel: '155555555', love: ['peach','waxberry']}) 2 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) #輸出信心 3 > db.age01.find().pretty()
1 > db.age01.updateOne({name: 'lisi'}, {$set:{age: '28'}}) 2 { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
1 > db.age01.updateMany({age: {$gt: '15'}}, {$set:{tel: '188888888'}}) 2 { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
1 db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
1 db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
1 db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
1 db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
1 db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
1 db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );
1 db.collection.remove( 2 <query>, 3 { 4 justOne: <boolean>, 5 writeConcern: <document> 6 } 7 )
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.find().pretty()
1 > db.age01.deleteOne({"name" : "xiaoming"}) 2 WriteResult({ "nRemoved" : 1 }) 3 > db.age01.find().pretty()
1 > db.age01.deleteMany({tel: '133333333'}) 2 { "acknowledged" : true, "deletedCount" : 2 }
1 > db.collection.deleteMany({})
1 db.collection.find(query, projection)
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.find().pretty() #pretty() 方法以格式化的方式來顯示全部文檔
1 > db.collection.find({key1:value1, key2:value2}).pretty() 2 > db.age01.find({age: {$gt:'10'}, tel: '188888888'}).pretty()
1 > db.col.find( 2 { 3 $or: [ 4 {key1: value1}, {key2:value2} 5 ] 6 } 7 ).pretty() 8 > db.age01.find({$or:[{age: {$gt: '14'}},{tel: '188888888'}]}).pretty()
1 > db.age01.find({age: {$gt: '10'},$or:[{name: 'zhangsan'},{tel: '188888888'}]}).pretty()
1 > db.age01.find({age: {$gt: '14',$lt: '28'}}).pretty()
1 > db.age01.find({name: 'zhangsan'},{name: 1, age: 1}).pretty() #僅顯示name和age 2 { 3 "_id" : ObjectId("5ceb9b2785cdc85ba0f3d6a3"), 4 "name" : "zhangsan", 5 "age" : "14" 6 } 7 > db.age01.find({name: 'zhangsan'},{name: 0}).pretty() #不顯示name 8 { 9 "_id" : ObjectId("5ceb9b2785cdc85ba0f3d6a3"), 10 "age" : "14", 11 "tel" : "155555555", 12 "love" : [ 13 "peach", 14 "waxberry" 15 ] 16 }
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.find({age: {$gt: '20'}}).pretty()
1 > db.age01.find({age: {$gte: '15'}}).pretty()
1 > db.age01.find({age: {$lt: '15'}}).pretty()
1 > db.age01.find({age: {$lte: '15'}}).pretty()
1 > db.age01.find({age: {$lt: '18',$gt: '10'}}).pretty()
1 > db.age01.find({love: /apple/}).pretty()
1 > db.age01.find({tel: /^188/}).pretty()
1 > db.age01.find({tel: /88$/}).pretty()
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.find({tel: {$type: 2}}).pretty() 4 > db.age01.find({tel: {$type: 'string'}}).pretty()
1 >db.COLLECTION_NAME.find().limit(NUMBER) 2 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 3 > use mydb 4 > db.age01.find({tel: /^188/}).limit(1).pretty()
1 > db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) 2 > db.age01.find({love: /apple/}).pretty()
1 > db.age01.find({love: /apple/}).limit(2).skip(1).pretty()
1 > db.COLLECTION_NAME.find().sort({KEY:1}) 2 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 3 > use mydb 4 > db.age01.find().sort({age: 1}).pretty()
1 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin 2 > use mydb 3 > db.age01.find({},{name: 1, _id: 0}).limit(2)
說明:sql
第一個 {} 爲 where 條件,爲空表示返回集合中全部文檔。mongodb
第二個 {} 指定那些列顯示和不顯示 (0表示不顯示 1表示顯示)。數據庫
11.2 範圍讀取服務器
想要讀取從 10 條記錄後 100 條記錄,至關於 sql 中limit (10,100)。網絡
1 > db.COLLECTION_NAME.find().skip(10).limit(100)
以上實例在集合中跳過前面 10 條返回 100 條數據。數據結構
提示:skip 和 limit 結合就能實現分頁。app
kip和limit方法只適合小數據量分頁,若是是百萬級效率就會很是低,由於skip方法是一條條數據數過去的,建議使用where_limit。不要輕易使用Skip來作查詢,不然數據量大了就會致使性能急劇降低,這是由於Skip是一條一條的數過來的,多了天然就慢了。ide
limit(n) 是用來規定顯示的條數,而 skip(n) 是用來在符合條件的記錄中從第一個記錄跳過的條數,這兩個函數能夠交換使用。函數
好比:find({},{age:1,_id:0}).limit(2).skip(1),在符合條件的文檔中,要顯示兩條文檔,顯示的位置從跳過第一條記錄開始。這樣不是很好理解。性能
若是寫成 find({},{age:1,_id:0}).skip(1).limit(2),在符合條件的文檔中,先跳過第一條文檔,而後顯示兩條文檔,這樣比較好理解。
注意:普通查詢skip,和limit三者執行順序和位置無關,可是在聚合aggregate中使用的時候,具備管道流的特質,執行順序是按照位置關係順序執行的。
參考文檔:https://www.runoob.com/mongodb/