《二》mongodb的使用

1、新建一个数据库
  • 直接 ->use mydb ,然后往里面存数据,数据库才会存在,不然是是不会存在的
2、往数据库里插入数据(首先需要切换到指定数据库)
  • 直接插入
> db.person.save({name:'liuming', age:30});
WriteResult({ "nInserted" : 1 })
> db.person.find();
{ "_id" : ObjectId("5bac9219174ea72515956a2e"), "name" : "liuming", "age" : 30 }
  • 使用语句插入
> for(var i=1;i<10;i++)db.thins.save({x:4;j:i});
> db.thins.find();
{ "_id" : ObjectId("5bac96ba174ea72515956a2f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5bac96ba174ea72515956a30"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5bac96ba174ea72515956a31"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5bac96ba174ea72515956a32"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5bac96ba174ea72515956a33"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5bac96ba174ea72515956a34"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5bac96ba174ea72515956a35"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5bac96ba174ea72515956a36"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5bac96ba174ea72515956a37"), "x" : 4, "j" : 9 }

注意:默认查出来的是10条,若想继续查询下面的数据,可以使用“it”命令
3、查询记录
  • 普通查询> db.thins.find();
  • 带条件查询,条件是name为sonzghen,结果显示age
    > db.thins.find({name:"sonzghen"}, {age:true}); { "_id" : ObjectId("5bac9a92174ea72515956a39"), "age" : 18 }
  • limit参数限制结果集数量
db.thins.find().limit(3); 结果集显示三条
  • sort参数排序
> db.thins.find().sort({j:1});  结果以 j 升序排列
> db.thins.find().sort({j:-1}); 结果以 j 降序排列
4、修改记录
$set:
> db.thins.update({name:"sonzghen"}, {$set:{name:"lily"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.thins.find({name:"lily"});
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
> db.thins.find({name:"sonzghen"});

$inc increase的缩写
> db.thins.update({name:"lily"},{$inc:{age:20}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.thins.find({name:"lily"});
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 38 }

upsert操作:如果不存在就新增,将update的第三个参数设为true
> db.thins.update({name:"json"}, {$inc:{age:1}}, true);
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("5baef59c2e10904eafa6a7de")
})
> db.thins.find({name:"json"});
{ "_id" : ObjectId("5baef59c2e10904eafa6a7de"), "name" : "json", "age" : 1 }
5 、删除记录
> db.thins.remove({x:4});
WriteResult({ "nRemoved" : 9 })
> db.thins.find();
{ "_id" : ObjectId("5bac9a83174ea72515956a38"), "x" : 1, "j" : 0 }
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
> 
6、高级查询
  • 条件操作符
> db.thins.find({x:{$gt:1}});
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
注释:$gt(>), $lt(<),  $gte(>=),  $lte(<=)
  • $all匹配所有
> db.users.find({age : {$all : [6, 8]}}); 只能查找出6和8都满足的记录 
  • $exists判断字段是否存在
> db.thins.find({age:{$exists:true}});
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
  • Null值处理
> db.thins.find({age:null});
{ "_id" : ObjectId("5bac9a83174ea72515956a38"), "x" : 1, "j" : 0 }
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
> db.thins.find({age:{$in:[null], $exists:true}});
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
> 
注意:当查找age为null的记录时,没有age这个字段的记录也会被查出来,所以需要加上$exists来判断是否存在
  • $mod取模运算
查询age取模10等于0的数据
> db.thins.find({age: {$mod:[10,0]}});
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
  • $ne 不等于
查询age不等于20的数据
> db.thins.find({age: {$ne:20}});
{ "_id" : ObjectId("5bac9a83174ea72515956a38"), "x" : 1, "j" : 0 }
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
  • $nin不包含,如果是in就是包含,还有 or或的关系
> db.thins.find({x: {$in:[1,3]}});
{ "_id" : ObjectId("5bac9a83174ea72515956a38"), "x" : 1, "j" : 0 }
> db.thins.find({x: {$nin:[1,3]}});
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
> db.thins.find({$or:[{name:"Tom"}, {name:"Bob"}]});
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }

  • $size数组元素个数
查询x的数组中的个数为3的数据
> db.thins.find({x:{$size:3}});
{ "_id" : ObjectId("5bacae49174ea72515956a3d"), "x" : [ 1, 2, 4 ] }

  • $count查询记录条数
> db.thins.find().count();
7
  • $skip限制记录的起点
> db.thins.find();
{ "_id" : ObjectId("5bac9a83174ea72515956a38"), "x" : 1, "j" : 0 }
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
{ "_id" : ObjectId("5bacae49174ea72515956a3d"), "x" : [ 1, 2, 4 ] }
{ "_id" : ObjectId("5bacae4d174ea72515956a3e"), "x" : [ 1, 2, 4, 5 ] }
> db.thins.find().skip(1);
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
{ "_id" : ObjectId("5bacae49174ea72515956a3d"), "x" : [ 1, 2, 4 ] }
{ "_id" : ObjectId("5bacae4d174ea72515956a3e"), "x" : [ 1, 2, 4, 5 ] }
> db.thins.find().skip(2);
{ "_id" : ObjectId("5baca0e2174ea72515956a3a"), "x" : 2, "j" : 1 }
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
{ "_id" : ObjectId("5bacae49174ea72515956a3d"), "x" : [ 1, 2, 4 ] }
{ "_id" : ObjectId("5bacae4d174ea72515956a3e"), "x" : [ 1, 2, 4, 5 ] }
  • 正则表达式
查询name以T开头,以m结尾的记录
> db.thins.find({name:/^T/, name:/m$/});
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
  • JS使用
> db.thins.find({$where:function(){return this.name=="Bob"}});
{ "_id" : ObjectId("5baca77f174ea72515956a3c"), "name" : "Bob", "age" : null }
7、存储过程
  • 存储过程是存储在db.system.js表中
  • 创建存储过程并调用
> db.system.js.save({_id:"get_thins_count", value:function(){return db.thins.count();}});
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : "get_thins_count"
})
> db.eval("get_thins_count()");
WARNING: db.eval is deprecated
7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值