Mongdb的常用命令



User youDb 當創建一個集合的時候會自動創建當前數據庫

Show dbs   查詢所有數據

db.dropDatabase();刪除當前使用的數據

db.cloneDatabase(“127.0.0.1”);指定機器上的數據庫的數據克隆到當前數據庫

db.copyDatabase(“mydb”,”temp”,”127.0.0.1”);將本機色mydb的數據複製到temp數據庫中

db.repairDatabase  修復當前的數據庫

db.getName();     查看當前使用的數據庫

db.stats()         顯示當前db狀態

db.version();      當前的db版本

db.getMongo();    查看當前的鏈接機器的地址

db.createCollection(“collName”,{size:20,capped:5,max:100});//創建成功會顯示{“ok”:1}

db.colName.isCapped();//判斷集合是否爲定容器

db.getCollection(“account”);

db.getCollectionNames();    得到當前的所有聚集集合

db.printColectionStats();     顯示當前db所有聚集索引的狀態

三:用戶相關

Db.addUser(“name”);

db.addUser(„userName,“pwd123“,true);//添加用戶,密碼是否可讀

db.auth(“userName“,“123123“);數據庫認證,安全模式

show users   顯示當前用戶

db.removeUser(“userName”);刪除用戶

四,聚集集合查詢

 

Mysql:  select * fromuserInfo;   //查詢所有記錄

Db:     db.userInfo.find();

Mysql:   select distictname from userInfo;   //去除name相同的數據

Db:      db.userInfo.distionct(“name”);

Mysql:  select * fromuserInfo where age=22;  //查詢age=22的記錄

Db:     db.userInfo.find({“age”:22});

Mysql:   select * fromuserInfo where age <22;  //查詢age小於22

Db     db.userInfo.find({age:{$lt:22}});

Mysql:   select * fromuserInfo where age >22;  //查詢age大於22

Db     db.userInfo.find({age:{$gt:22}});

Mysql:     select * fromuserInfo where age >=25;//查詢age>=25

Db:       db.userInfo.find({age:{$gte:25}});

Mysql:     select * fromuserInfo where age <=25;//查詢age<=25

Db:       db.userInfo.find({age:{$lte:25}});

Mysql: select * from user where age>=23adn age<=26 //age>=23並且age<=26

Db:      db.userInfo.find({age:{$gte:23,$lte:26}});

Mysql:    select *f romuserInfo where  name like ‘%%’//模糊查詢張

Db:       db.userInfo.find({name://});

Mysql:   select * fromuserInfo where name like ‘%’;     //以張開頭的

Db:     db.userInfo.find({name:/^/});

Mysql:    selectname,age from userInfo;        //指定某個字段

Db:  db.userInfo.find({},{name:1,age:1});

Msql: select name,agefrom userInfo where age>25;      //查詢指定列name,age數據age>25

Db:   db.userInfo.find({age:{$gt:25}},{name:1,age:1});

Db:   db.userInfo.find().sort({age:1});   //按照年齡升序排列

      db.userInfo.find.sort({age:-1});   //按照年齡降續排列

Mysql: select * from userInfo where name=“張三“ and age=““;//查詢name = zhangsan, age= 22

Db:     db.userInfo.find({name:’張三’,age:22});

Mysql:  select top 5 * fromuserInfo;     //顯示5條數據

Db:     db.userInfo.find.limit(5);

Mysql: select * fromuserInfo where id not in (select top 10 * from userInfo);//十條以後的數據

Db:    db.userInfo.find().skip(10);

Mysql: select * fromuserInfo limite 10,5;//查詢5-10之間的數據

Db:    db.userInfo.find..limit(10).skip(5);    //pageSize=limit()

Mysql:  select * from userInfo where age =22 orage=25; //查詢age2225

DB:    db.userInfo.find({$or:[{age:22},{age:25}]});

Mysql:  select top1 * from userInfo;            //查詢一條數據

Db:     db.userInfo.findOne()  或者 db.userInfo.find.limit(1);

Mysql:  select count(*)from userInfo where age>=20; //查詢某條記錄的結果集

Db:     db.userInfo.find({age:{$gte:25}}).count();

Msql:   selectcount(sex) from userInfo;     //某列進行排序

Db:     db.userInfo.find({sex:{$exists:true}}).count();

五:索引

db.userInfo.ensureIndex({name:1});    //創建 索引

db.userInfo.ensureIndex({name:1,ts:-1});

db.userInfo.getIndexes();           //查詢當前聚集集合所有索引

db.userInfo.totalIndexSize();       //查詢總索引的記錄的大小

db.users.reIndex();                //讀取當前集合的所有index信息

db.users.dropIndex("name_1");      刪除指定索引

db.users.dropIndexes();           刪除所有索引索引

六、修改、添加、刪除集合數據

db.users.save({name: ‘zhangsan', age: 25, sex: true});  添加//添加的數據的數據列,沒有固定,根據添加的數據爲準

db.users.update({age: 25}, {$set: {name: 'changeName'}}, false,true);   //修改

相當於:update users set name =changeName' whereage = 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';

db.users.remove({age: 132});  刪除

db.users.findAndModify({  //查詢修改刪除

    query: {age: {$gte: 25}},

    sort: {age: -1},

    update: {$set: {name: 'a2'}, $inc: {age:2}},

    remove: true

});

db.runCommand({ findandmodify : "users",

    query: {age: {$gte: 25}},

    sort: {age: -1},

    update: {$set: {name: 'a2'}, $inc: {age:2}},

    remove: true

});

update remove其中一個是必須的參數; 其他參數可選。
參數   詳解    默認值
query   
查詢過濾條件
    {}
sort   
如果多個文檔符合查詢過濾條件,將以該參數指定的排列方式選擇出排在首位的對象,該對象將被操作
    {}
remove   
若爲true,被選中對象將在返回前被刪除
    N/A
update   
一個 修改器對象

N/A
new   
若爲true,將返回修改後的對象而不是原始對象。在刪除操作中,該參數被忽略。    false
fields   
參見
Retrieving a Subset ofFields (1.5.0+)
All fields
upsert   
創建新對象若查詢結果爲空。 示例
(1.5.4+)
false

七、語句塊操作
1
、簡單Hello World

print("Hello World!");

將一個對象轉換成json

tojson(new Object());

tojson(new Object('a'));

3、循環添加數據

> for (var i = 0; i < 30; i++) {

... db.users.save({name: "u_" + i, age: 22 + i, sex:i % 2});

... };這樣就循環添加了30條數據,同樣也可以省略括號的寫法

for (var i = 0; i < 30; i++) db.users.save({name:"u_" + i, age: 22 + i, sex: i % 2});

也是可以的,當你用db.users.find()查詢的時候,顯示多條數據而無法一頁顯示的情況下,可以用it查看下一頁的信息;

4find遊標查詢

>var cursor = db.users.find();

> while (cursor.hasNext()) {

    printjson(cursor.next());

}

這樣就查詢所有的users信息,同樣可以這樣寫

var cursor = db.users.find();

while (cursor.hasNext()) { printjson(cursor.next); }//同樣可以省略{}

5forEach迭代循環// forEach中必須傳遞一個函數來處理每條迭代的數據信息

db.users.find().forEach(printjson);

6、將find遊標當數組處理

var cursor = db.users.find();

cursor[4];

取得下標索引爲4的那條數據
既然可以當做數組處理,那麼就可以獲得它的長度:cursor.length();或者cursor.count();
那樣我們也可以用循環顯示數據

for (var i = 0, len = c.length(); i < len; i++)printjson(c[i]);

7、將find遊標轉換成數組

> var arr = db.users.find().toArray();

> printjson(arr[2]);

toArray方法將其轉換爲數組

8、定製我們自己的查詢結果

只顯示age <= 28的並且只顯示age這列數據

db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);

db.users.find({age: {$lte: 28}}, {age:true}).forEach(printjson);

排除age的列

db.users.find({age: {$lte: 28}}, {age:false}).forEach(printjson);

9forEach傳遞函數顯示信息

db.things.find({x:4}).forEach(function(x) {print(tojson(x));});

查看聚集集合基本信息
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();

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的值爲1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章