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); 结果集显示三条
> 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(<=)
> db.users.find({age : {$all : [6, 8]}}); 只能查找出6和8都满足的记录
> db.thins.find({age:{$exists:true}});
{ "_id" : ObjectId("5bac9a92174ea72515956a39"), "name" : "lily", "age" : 18 }
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
> 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来判断是否存在
查询age取模10等于0的数据
> db.thins.find({age: {$mod:[10,0]}});
{ "_id" : ObjectId("5baca116174ea72515956a3b"), "name" : "Tom", "age" : 20, "phone" : "13327098769" }
查询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 }
查询x的数组中的个数为3的数据
> db.thins.find({x:{$size:3}});
{ "_id" : ObjectId("5bacae49174ea72515956a3d"), "x" : [ 1, 2, 4 ] }
> db.thins.find().count();
7
> 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" }
> 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