mongodb安裝及簡單操作附截圖_第1頁
mongodb安裝及簡單操作附截圖_第2頁
mongodb安裝及簡單操作附截圖_第3頁
mongodb安裝及簡單操作附截圖_第4頁
mongodb安裝及簡單操作附截圖_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、MongoDB安裝及簡單操作1.下載mongodb鏈接: 密碼:9af8 這是我網(wǎng)盤的鏈接,下載解壓到D盤2.配置文件第一步,解壓之后將文件夾放在D盤;第二步,將mongodb-win32-x86_64-2.0.6文件夾重命名為mongodb;第三步,在D:mongodb下創(chuàng)建data文件夾,路徑D:mongodb下包含下面文件;第四步,在data文件夾下創(chuàng)建db文件夾和log文件夾如下圖;第五步,在log文件夾下創(chuàng)建日志文件MongoDB.log如下圖3.連接數(shù)據(jù)庫打開命令提示符(管理員權(quán)限)D:cd d:mongodbbinmongod -dbpath "d:mongodbdat

2、adb"正常情況下會看到下面的界面這時候,已經(jīng)成功打開mongodb的服務了,27017是默認端口。在瀏覽器中檢驗一下是否真的成功的啟動了mongodb的服務,在瀏覽器輸入http:/localhost:27017/只要看到上面的界面就證明連接成功了。這時候,再打開一個命令提示符窗口,可以不是管理員權(quán)限,去進行數(shù)據(jù)庫的操作。輸入mongo輸入show dbs插入一條數(shù)據(jù)db.user.insert(name:”laowang”)也可以是db.user.save(name:”laowang”)或者,一次添加姓名和年齡數(shù)據(jù),二維數(shù)據(jù)db.user.save(name:”laowang”,

3、age:”25”)查看數(shù)據(jù)db.user.find() 相當于sql的select * from user其他的mongodb常用操作命令,我在網(wǎng)上找了一些,直接給你粘貼上來。1、Help查看命令提示 help  db.help();  db.yourColl.help();  db.youColl.find().help();  rs.help();2、切換/創(chuàng)建數(shù)據(jù)庫 use yourDB;  當創(chuàng)建一個集合(table)的時候會自動創(chuàng)建當前數(shù)據(jù)庫3、查詢所有數(shù)據(jù)庫 show dbs;4、刪除當前使用數(shù)據(jù)庫&#

4、160;db.dropDatabase();5、從指定主機上克隆數(shù)據(jù)庫 db.cloneDatabase(“”); 將指定機器上的數(shù)據(jù)庫的數(shù)據(jù)克隆到當前數(shù)據(jù)庫6、從指定的機器上復制指定數(shù)據(jù)庫數(shù)據(jù)到某個數(shù)據(jù)庫 db.copyDatabase("mydb", "temp", "");將本機的mydb的數(shù)據(jù)復制到temp數(shù)據(jù)庫中7、修復當前數(shù)據(jù)庫 db.repairDatabase();8、查看當前使用的數(shù)據(jù)庫 db.getName(); db; db和g

5、etName方法是一樣的效果,都可以查詢當前使用的數(shù)據(jù)庫9、顯示當前db狀態(tài) db.stats();10、當前db版本 db.version();11、查看當前db的鏈接機器地址 db.getMongo();Collection聚集集合1、創(chuàng)建一個聚集集合(table) db.createCollection(“collName”, size: 20, capped: 5, max: 100);2、得到指定名稱的聚集集合(table) db.getCollection("account");3、得到當前db的所有聚集集合&#

6、160;db.getCollectionNames();4、顯示當前db所有聚集索引的狀態(tài) db.printCollectionStats();用戶相關(guān)1、添加一個用戶 db.addUser("name"); db.addUser("userName", "pwd123", true); 添加用戶、設(shè)置密碼、是否只讀2、數(shù)據(jù)庫認證、安全模式 db.auth("userName", "123123");3、顯示當前所有用戶 show users;4

7、、刪除用戶 db.removeUser("userName");其他1、查詢之前的錯誤信息 db.getPrevError();2、清除錯誤記錄 db.resetError();查看聚集集合基本信息1、查看幫助  db.yourColl.help();2、查詢當前集合的數(shù)據(jù)條數(shù)  db.yourColl.count();3、查看數(shù)據(jù)空間大小 db.userInfo.dataSize();4、得到當前聚集集合所在的db db.userInfo.getDB();5、得到當前聚集的狀態(tài) db.userInfo.stats();6、

8、得到聚集集合總大小 db.userInfo.totalSize();7、聚集集合儲存空間大小 db.userInfo.storageSize();8、Shard版本信息  db.userInfo.getShardVersion()9、聚集集合重命名 db.userInfo.renameCollection("users"); 將userInfo重命名為users10、刪除當前聚集集合 db.userInfo.drop();聚集集合查詢1、查詢所有記錄db.userInfo.find();相當于:select* from userInfo;默認每頁顯示20條記錄,當

9、顯示不下的情況下,可以用it迭代命令查詢下一頁數(shù)據(jù)。注意:鍵入it命令不能帶“;”但是你可以設(shè)置每頁顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。 2、查詢?nèi)サ艉蟮漠斍熬奂现械哪沉械闹貜蛿?shù)據(jù)db.userInfo.distinct("name");會過濾掉name中的相同數(shù)據(jù)相當于:select distict name from userInfo; 3、查詢age = 22的記錄db.userInfo.find("age": 22);相當于: select * from us

10、erInfo where age = 22; 4、查詢age > 22的記錄db.userInfo.find(age: $gt: 22);相當于:select * from userInfo where age >22; 5、查詢age < 22的記錄db.userInfo.find(age: $lt: 22);相當于:select * from userInfo where age <22; 6、查詢age >= 25的記錄db.userInfo.find(age: $gte: 25);相當于:select * from userI

11、nfo where age >= 25; 7、查詢age <= 25的記錄db.userInfo.find(age: $lte: 25); 8、查詢age >= 23 并且 age <= 26db.userInfo.find(age: $gte: 23, $lte: 26); 9、查詢name中包含 mongo的數(shù)據(jù)db.userInfo.find(name: /mongo/);/相當于%select * from userInfo where name like %mongo%; 10、查詢name中以mongo開頭的db.us

12、erInfo.find(name: /mongo/);select * from userInfo where name like mongo%; 11、查詢指定列name、age數(shù)據(jù)db.userInfo.find(, name: 1, age: 1);相當于:select name, age from userInfo;當然name也可以用true或false,當用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列信息。 12、查詢指定列name、age數(shù)據(jù), age > 25db.userInfo.find(age:

13、$gt: 25, name: 1, age: 1);相當于:select name, age from userInfo where age >25; 13、按照年齡排序升序:db.userInfo.find().sort(age: 1);降序:db.userInfo.find().sort(age: -1); 14、查詢name = zhangsan, age = 22的數(shù)據(jù)db.userInfo.find(name: 'zhangsan', age: 22);相當于:select * from userInfo where name = zhang

14、san and age = 22; 15、查詢前5條數(shù)據(jù)db.userInfo.find().limit(5);相當于:selecttop 5 * from userInfo; 16、查詢10條以后的數(shù)據(jù)db.userInfo.find().skip(10);相當于:select * from userInfo where id not in (selecttop 10 * from userInfo); 17、查詢在5-10之間的數(shù)據(jù)db.userInfo.find().limit(10).skip(5);可用于分頁,limit是pageSize,skip是第幾

15、頁*pageSize 18、or與 查詢db.userInfo.find($or: age: 22, age: 25);相當于:select * from userInfo where age = 22 or age = 25; 19、查詢第一條數(shù)據(jù)db.userInfo.findOne();相當于:selecttop 1 * from userInfo;db.userInfo.find().limit(1); 20、查詢某個結(jié)果集的記錄條數(shù)db.userInfo.find(age: $gte: 25).count();相當于:select count(*) fr

16、om userInfo where age >= 20; 21、按照某列進行排序db.userInfo.find(*: $exists: true).count();相當于:select count(*) from userInfo;索引1、創(chuàng)建索引db.userInfo.ensureIndex(name: 1);db.userInfo.ensureIndex(name: 1, ts: -1); 2、查詢當前聚集集合所有索引db.userInfo.getIndexes(); 3、查看總索引記錄大小db.userInfo.totalIndexSize();&#

17、160;4、讀取當前集合的所有index信息db.users.reIndex(); 5、刪除指定索引db.users.dropIndex("name_1"); 6、刪除所有索引索引db.users.dropIndexes();修改、添加、刪除集合數(shù)據(jù)1、添加db.users.save(name: zhangsan, age: 25, *: true);添加的數(shù)據(jù)的數(shù)據(jù)列,沒有固定,根據(jù)添加的數(shù)據(jù)為準 2、修改db.users.update(age: 25, $set: name: 'changeName', false, tru

18、e);相當于:update users set name = changeName where age = 25; db.users.update(name: 'Lisi', $inc: age: 50, false, true);相當于:update users set age = age + 50 where name = Lisi; db.users.update(name: 'Lisi', $inc: age: 50, $set: name: 'hoho', false, true);相當于:update users

19、set age = age + 50, name = hoho where name = Lisi; 3、刪除db.users.remove(age: 132); 4、查詢修改刪除db.users.findAndModify(    query: age: $gte: 25,     sort: age: -1,     update: $set: name: 'a2', $inc: age: 2,    remove: true); db.runC

20、ommand( findandmodify : "users",     query: age: $gte: 25,     sort: age: -1,     update: $set: name: 'a2', $inc: age: 2,    remove: true);update 或 remove 其中一個是必須的參數(shù); 其他參數(shù)可選。參數(shù)詳解默認值query查詢過濾條件sort如果多個文檔符合查詢過濾條件,將以該參數(shù)指定的排列方式選擇出排

21、在首位的對象,該對象將被操作remove若為true,被選中對象將在返回前被刪除N/Aupdate一個 修改器對象N/Anew若為true,將返回修改后的對象而不是原始對象。在刪除操作中,該參數(shù)被忽略。falsefields參見Retrieving a Subset of Fields (1.5.0+)All fieldsupsert創(chuàng)建新對象若查詢結(jié)果為空。 示例 (1.5.4+)false語句塊操作1、簡單Hello Worldprint("Hello World!");這種寫法調(diào)用了print函數(shù),和直接寫入"Hello World!"的效果是一樣

22、的; 2、將一個對象轉(zhuǎn)換成jsontojson(new Object();tojson(new Object('a'); 3、循環(huán)添加數(shù)據(jù)> for (var i = 0; i < 30; i+) . db.users.save(name: "u_" + i, age: 22 + i, *: i % 2);. ;這樣就循環(huán)添加了30條數(shù)據(jù),同樣也可以省略括號的寫法> for (var i = 0; i < 30; i+) db.users.save(name: "u_" + i, age: 22 + i, *: i % 2);也是可以的,當你用db.users.find()查詢的時候,顯示多條數(shù)據(jù)而無法一頁顯示的情況下,可以用it查看下一頁的信息; 4、find 游標查詢>var cursor = db.users.find();> while (cursor.hasNext()      printjson(cursor.next(); 這樣就查詢所有的users信息,同樣可以這樣寫var cursor = db.use

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論