日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MongoDB命令--自用记录

發布時間:2025/3/19 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MongoDB命令--自用记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
成功啟動MongoDB后,再打開一個命令行窗口輸入mongo,就可以進行數據庫的一些操。 輸入help可以看到基本操作命令: show dbs:顯示數據庫列表 show collections:顯示當前數據庫中的集合(類似關系數據庫中的表) show users:顯示用戶 use <db name>:切換當前數據庫,這和MS-SQL里面的意思一樣 db.help():顯示數據庫操作命令,里面有很多的命令 db.foo.help():顯示集合操作命令,同樣有很多的命令,foo指的是當前數據庫下,一個叫foo的集合,并非真正意義上的命令 db.foo.find():對于當前數據庫中的foo集合進行數據查找(由于沒有條件,會列出所有數據) db.foo.find( { a : 1 } ):對于當前數據庫中的foo集合進行查找,條件是數據中有一個屬性叫a,且a的值為1MongoDB沒有創建數據庫的命令,但有類似的命令。 如:如果你想創建一個“myTest”的數據庫,先運行use myTest命令,之后就做一些操作(如:db.createCollection('user')),這樣就可以創建一個名叫“myTest”的數據庫。數據庫常用命令 1、Help查看命令提示helpdb.help();db.yourColl.help();db.youColl.find().help();rs.help(); 2、切換/創建數據庫use yourDB; 當創建一個集合(table)的時候會自動創建當前數據庫 3、查詢所有數據庫show dbs; 4、刪除當前使用數據庫db.dropDatabase(); 5、從指定主機上克隆數據庫db.cloneDatabase(“127.0.0.1”); 將指定機器上的數據庫的數據克隆到當前數據庫 6、從指定的機器上復制指定數據庫數據到某個數據庫db.copyDatabase("mydb", "temp", "127.0.0.1");將本機的mydb的數據復制到temp數據庫中 7、修復當前數據庫db.repairDatabase(); 8、查看當前使用的數據庫db.getName();db; db和getName方法是一樣的效果,都可以查詢當前使用的數據庫 9、顯示當前db狀態db.stats(); 10、當前db版本db.version(); 11、查看當前db的鏈接機器地址db.getMongo(); Collection聚集集合 1、創建一個聚集集合(table)db.createCollection(“collName”, {size: 20, capped: 5, max: 100}); 2、得到指定名稱的聚集集合(table)db.getCollection("account"); 3、得到當前db的所有聚集集合db.getCollectionNames(); 4、顯示當前db所有聚集索引的狀態db.printCollectionStats();用戶相關 1、添加一個用戶db.addUser("name");db.addUser("userName", "pwd123", true); 添加用戶、設置密碼、是否只讀 2、數據庫認證、安全模式db.auth("userName", "123123"); 3、顯示當前所有用戶show users; 4、刪除用戶db.removeUser("userName");其他 1、查詢之前的錯誤信息db.getPrevError(); 2、清除錯誤記錄db.resetError();查看聚集集合基本信息 1、查看幫助 db.yourColl.help(); 2、查詢當前集合的數據條數 db.yourColl.count(); 3、查看數據空間大小 db.userInfo.dataSize(); 4、得到當前聚集集合所在的db db.userInfo.getDB(); 5、得到當前聚集的狀態 db.userInfo.stats(); 6、得到聚集集合總大小 db.userInfo.totalSize(); 7、聚集集合儲存空間大小 db.userInfo.storageSize(); 8、Shard版本信息 db.userInfo.getShardVersion() 9、聚集集合重命名 db.userInfo.renameCollection("users"); 將userInfo重命名為users 10、刪除當前聚集集合 db.userInfo.drop();聚集集合查詢 1、查詢所有記錄 db.userInfo.find(); 相當于:select* from userInfo; 默認每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁數據。注意:鍵入it命令不能帶“;” 但是你可以設置每頁顯示數據的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。 2、查詢去掉后的當前聚集集合中的某列的重復數據 db.userInfo.distinct("name"); 會過濾掉name中的相同數據 相當于:select distict name from userInfo; 3、查詢age = 22的記錄 db.userInfo.find({"age": 22}); 相當于: select * from userInfo 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 userInfo where age >= 25; 7、查詢age <= 25的記錄 db.userInfo.find({age: {$lte: 25}}); 8、查詢age >= 23 并且 age <= 26 db.userInfo.find({age: {$gte: 23, $lte: 26}}); 9、查詢name中包含 mongo的數據 db.userInfo.find({name: /mongo/}); //相當于%% select * from userInfo where name like ‘%mongo%’; 10、查詢name中以mongo開頭的 db.userInfo.find({name: /^mongo/}); select * from userInfo where name like ‘mongo%’; 11、查詢指定列name、age數據 db.userInfo.find({}, {name: 1, age: 1}); 相當于:select name, age from userInfo; 當然name也可以用truefalse,當用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列信息。 12、查詢指定列name、age數據, age > 25 db.userInfo.find({age: {$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的數據 db.userInfo.find({name: 'zhangsan', age: 22}); 相當于:select * from userInfo where name = ‘zhangsan’ and age = ‘22’; 15、查詢前5條數據 db.userInfo.find().limit(5); 相當于:selecttop 5 * from userInfo; 16、查詢10條以后的數據 db.userInfo.find().skip(10); 相當于:select * from userInfo where id not in ( selecttop 10 * from userInfo ); 17、查詢在5-10之間的數據 db.userInfo.find().limit(10).skip(5); 可用于分頁,limit是pageSize,skip是第幾頁*pageSize 18、or與 查詢 db.userInfo.find({$or: [{age: 22}, {age: 25}]}); 相當于:select * from userInfo where age = 22 or age = 25; 19、查詢第一條數據 db.userInfo.findOne(); 相當于:selecttop 1 * from userInfo; db.userInfo.find().limit(1); 20、查詢某個結果集的記錄條數 db.userInfo.find({age: {$gte: 25}}).count(); 相當于:select count(*) from userInfo where age >= 20; 如果要返回限制之后的記錄數量,要使用count(true)或者count(非0) db.users.find().skip(10).limit(5).count(true); 21、按照某列進行排序 db.userInfo.find({sex: {$exists: true}}).count(); 相當于:select count(sex) from userInfo;索引 1、創建索引 db.userInfo.ensureIndex({name: 1}); db.userInfo.ensureIndex({name: 1, ts: -1}); 2、查詢當前聚集集合所有索引 db.userInfo.getIndexes(); 3、查看總索引記錄大小 db.userInfo.totalIndexSize(); 4、讀取當前集合的所有index信息 db.users.reIndex(); 5、刪除指定索引 db.users.dropIndex("name_1"); 6、刪除所有索引索引 db.users.dropIndexes();修改、添加、刪除集合數據 1、添加 db.users.save({name: ‘zhangsan’, age: 25, sex: true}); 添加的數據的數據列,沒有固定,根據添加的數據為準 2、修改 db.collection.update(criteria, objNew, upsert, multi ) criteria:update的查詢條件,類似sql update查詢內where后面的 objNew:update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內set后面的。 upsert : 如果不存在update的記錄,是否插入objNew,true為插入,默認是false,不插入。 multi : mongodb默認是false,只更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。 db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true); 相當于: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 set age = age + 50, name = ‘hoho’ where name = ‘Lisi’; 復制代碼

轉載于:https://juejin.im/post/5d0901696fb9a07ecf7227fb

總結

以上是生活随笔為你收集整理的MongoDB命令--自用记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。