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();

猜你喜欢

转载自www.cnblogs.com/DDgougou/p/10200281.html