1. 程式人生 > >MongoDB常用操作練習

MongoDB常用操作練習

最近在自學MongoDB,

 

連線資料庫
mongo.exe test

設定訪問限制後連線
mongo.exe -u root -p test

設定訪問限制
db.addUser("root","111111");
mongo.exe auth;

建立集合(資料庫)
db.createCollection("mydatabase",{size:1000000,max:100});

切換資料庫
use mydatabase;

 

建立表(student)
db.student.save({_id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]});
db.student.save({_id:2,classid:2,age:19,name:"little2",love:["play"]});
db.student.save({_id:3,classid:2,age:20,name:"little3"});
db.student.save({_id:4,classid:1,age:21,name:"little4"});
db.student.save({_id:5,classid:1,age:22,name:"little5",opt:"woshisheia"});
db.student.save({_id:6,classid:2,age:23,name:"23little4"});

 

查詢:
db.student.find();


//從第二條查尋,查出三條
db.student.find().skip(1).limit(3);


//查詢出name為little1的資料
db.student.find({name:"little1"});


//查詢出19<age<=21的資料
db.student.find({age:{$gt:19,$lte:21}});


//$mod:查詢出age為奇數的資料(對2求餘為1即為奇數)(這裡請注意[2,1]的位置不能錯)
db.student.find({age:{$mod:[2,1]}});


//$exists:查詢出存在opt欄位的資料,  存在true,不存在false
db.student.find({opt:{$exists:true}});    db.student.find({opt:{$exists:false}});


//查詢出name不為little2的資料
db.student.find({name:{$ne:"little2"}});


//$in:查詢出age為16,18,19的資料
db.student.find({age:{$in:[16,18,19]}});  

     
//$nin:查詢出age不為16,18,19的資料
db.student.find({age:{$nin:[16,18,19]}});


//$size:查詢出love有三個的資料
db.student.find({love:{$size:3}});


//查詢出name不是以litt開頭的資料
db.student.find({name:{$not:/^litt.*/}});


//查詢age>20的資料
db.student.find({age:{$gt:20}});

db.student.find({$where:"this.age>20"});

db.student.find("this.age>20");

var f = function(){return this.age>20};
db.student.find(f);


//sort:1升序,   -1降序
按age升序:db.student.find().sort({age:1});   按age降序:db.student.find().sort({age:-1});


//count:查詢資料條數
db.student.find().count();

 

//like:模糊查詢

db.student.find({name:/little/});    相當於 select * from student where name like "%little%";

db.student.find({name:/^little/});    相當於 select * from users where name like "little%";

 

//distinct:去重

db.student.distinct('name');

 

//type:屬性型別判斷

db.sutdent.find({"title" : {$type : 2}})         db.student.find({"title" : {$type : 'string'}})

 

 

 

 

 

//查詢 name為little1的學生,並且只顯示 age,name兩個欄位
db.student.find({name:"little1"},{name:1,age:1})       等價於 select name,age from student where name = "little1"

//and:查詢 name為little1,age為18的學生,並且只顯示 age,name,love三個欄位
db.student.find({name:"little1",age:18},{name:1,age:1,love:1})  等價於 select name,age,love from student where name = "little1" and age = 18

//or:查詢 name為little3或age為19的學生
db.student.find({'$or':[{name:'little3'},{age:19}]})  等價於 select * from student where name = "little3" or age = 19

//null:查詢age為null的資料

db.student.find({age:null})

 

//

 

 


刪除:
//刪除age為20的資料
db.student.remove({age:20});
//刪除表
db.student.drop();

 


儲存過程:
db.system.js.save({_id:"mypro",value:function(){
     var total = 0;
     var arr = db.student.find().toArray();
     for(var i=arr.length-1;i>=0;i--){
         total += arr[i].age;
     }
     return total;
}});
//查詢儲存過程
db.system.js.find();
//執行儲存過程
db.eval("mypro()");

 

 

分組統計(MapReduce):
var mapfun = function(){emit(this.classid,1)}
var reducefun = function(key, values){
      var x = 0;
      values.forEach(function(v){x+=v});
      return x;
}

var finafun = function(key, value){
     return {classid:key,count:value};
}

res = db.runCommand({
      mapreduce:"student",
      map:mapfun,
      reduce:reducefun,
      out:student_res,
      finalize:finafun
      //可加入條件
     //query:{age:{$gt:19}}
});

//查詢統計結果(分班級統計人數)
db.student_res.find();