(二)操作mongodb数据库
2016-03-20 23:04
411 查看
一、访问MongoDb
1、控制台步骤:cmd---->d:\mongo\bin\mongo,则可以开始数据的增删改查
执行后:默认连接数据库“test”库
命令:show dbs -----》展示所有的数据库
默认的连接是:127.0.0.1:27017
2、使用ip和端口访问
d:\mongo\bin\mongo 127.0.0.1:27017 ?(数据库名称)
命令:db---》显示当前所在的数据库名称
3、基本数据库命令
(1)help命令提示
db.help()
(2)切换数据库,如果数据库不存在则创建
use 数据库名
(3)显示所有数据库----》只会显示存在的数据库
show dbs
4、增删改操作
(1)进入数据库
use baidu
(2)插入命令
db.users.insert({name:"wang2",age:20})----->数据是Json形式的
(3)删除当前数据库
db.dropDatabase();
(4)显示当前db的状态
db.stats();
可查看当前数据库的详细信息
(5)显示db版本
db.version();
(6)查看当前db的连接机器地址
db.getMongo();
(7)脚本支持
conntection to 127.0.0.1:27017
var i=10;
if(true){
var a=11;
}
二、集合的相关命令
1、显示创建集合
显示:db.createCollection("usersname");
步骤:
use baichi
db.createCollection("customer");//在baichi库中创建集合customer
show collections
2、隐式创建集合
db.users.insert({name:"wang3",age:60})
users代表集合名称,json串是插入的数据
注:以上说明如果没有users集合,那么Mongodb会主动创建users集合、再插入一条数据
3、显示当前数据库中的所有集合
show collections 或 show tables
4、删除集合
db.users.drop();
注:users代表要删除的集合
三、文档的相关命令(MongoDB操作文档)
1、创建文档
db.users.insert({name:"wang3",age:23})
此操作将会自动生成一个全球唯一的主键:
时间戳(生成时间)+mongo机器(mongo所在的机器)+PID(进程号)+计数器(随机产生数)
db.users.find();-------->查看刚才所插入的数据
2、删除文档
db.users.remove({}) ------>如果没有花括号,则代表删除所有数据;如果有就代表有条件删除
db.users.remove({name:"wang3"}); -------------->删除name未wang2的数据
3、修改文档
格式:db.集合名.update({},{},0,1)
第一个参数:修改的条件,json类型
第二个参数:更新的命令,更新的内容
第三个参数:如果为发现符合更新条件的数据内容,是否进行插入操作;1代表是,0代表否
第四个参数:是否进行多行更新;1代表是,0代表否
举例:
(1)db.users.update({name:"wang3"},{$set:{password:"888"}},0,1); //所有name未suns的数据设置为"888"
(2)db.users.update({},{$ins:{age:7}},0,1);//$inc为对应key中的数据进行算术运算,即在age的原有基础上统一加7岁,减法则用-7
db.users.update({name:"wang3"},{$inc:{age:2}},0,1)
4、基本查询操作
(1)查询集合中的所有内容:db.users.find()
(2)查询集合中的第一个元素:db.users.findOne();
5、条件查询
(1)"=": db.users.find({name:"sun2"}) ---->找出name是sun2的数据
(2)"!="--->$ne(not equal) db.users.find({name:{"$ne":"sun2"}}) ---》找出name不是sun2的数据
(3)">"--->$gt(great than) db.users.find({age:{"$gt":10}}) --->找出age大于10的数据
(4)"<"---->$lt(less than) db.users.find({age:{"$lt":10}}) ---->找出age小于10的数据
(5)">="-->$gte(great than equal) db.users.find({age:{"$gte":10}}) --->找出age大于等于10的数据
(6)"<="--->$lte(less than equal) db.users.find({age:{"$lte":10}})--->找出age小于等于10的数据
6、逻辑运算
(1)与运算:即“并且”;举例----》db.users.find({name:"sun2",age:10})
(2)或运算:即“或者”,关键字$or、$nor
举例:db.users.find({$or:[{name:"sun2"},{age:32}]})
db.users.find({$nor:[{name:"sun2"},{age:23}]})
使用格式:db.users.find({$nor:[{?条件}]})
7、集合运算
(1)$in类似于关系数据库中的in操作
db.users.find({age:{$in:[12,23,24]}})---->找出age在12、23、24之中的数据
(2)$all主要用于查询数组中的包含关系,查询条件中只要有一个不包含就不返回
a、{"name":"bb1","post":[1,2,3]}
b、{"name":"bb1","post":[2,3,4]}
c、{"name":"bb1","post":[2,5,7]}
d 、{"name":"bb1","post":[3,9,10]
语句:db.users.find({post:{$all:[2,3]}})}---->查询结果是a、b符合条件
8、统计、分页和排序
(1)统计
db.users.count();//所有数据条数
db.users.find().count(); //完整的条数
db.users.find({age:{$lt:5}}).count();
(2)排序
db.users.find().sort({age:1}); ----->按age升序排序
db.users.find().sort({age:-1});----->按age降序排序
(3)分页
a、db.users.find().skip(?),limit(?);
第一个参数:代表从第几条数据开始(默认是0)
第二个参数:代表每页需要几条数据
举例:
db.users.find.sort({age:-1}).skip(2).limit(3);
b、忽略分页效果,获取总条数
db.users.find().sort({age:-1}).skip(2).limit(3).count(0);
c、记录分页效果,统计当前页的数量
db.users.find().sort({age:-1}).skip(2).limit(8).count(1);
9、特殊条件查询--》投影查询
db.users.find({},{_id:0})-->第一个参数是条件,第二个参数是决定显示什么键
$exists判断一个key是否存在
db.users.find({name:{$exists:1}})
例:db.users.find({},{_id:0,age:0})-------->表示不显示id和age键
10、mongodb的索引
(1)普通索引
db.collection(集合名).ensureIndex({key(需要设置索引的键名):1})
查询索引信息:db.collection.stats();
**mongodb会自动为“_id”的键添加索引
(2)删除索引
db.collection(集合名).dropIndex({key(需要删除的索引所在的键名):1})
**删除集合也会将键中的索引删除
(2)唯一索引命令
a、创建唯一索引
db.collection.ensureIndex({{key:1},{unique:true}})
查看:db.collection.stats();
b、删除索引
db.collection.dropIndex({kay:1})
1、控制台步骤:cmd---->d:\mongo\bin\mongo,则可以开始数据的增删改查
执行后:默认连接数据库“test”库
命令:show dbs -----》展示所有的数据库
默认的连接是:127.0.0.1:27017
2、使用ip和端口访问
d:\mongo\bin\mongo 127.0.0.1:27017 ?(数据库名称)
命令:db---》显示当前所在的数据库名称
3、基本数据库命令
(1)help命令提示
db.help()
(2)切换数据库,如果数据库不存在则创建
use 数据库名
(3)显示所有数据库----》只会显示存在的数据库
show dbs
4、增删改操作
(1)进入数据库
use baidu
(2)插入命令
db.users.insert({name:"wang2",age:20})----->数据是Json形式的
(3)删除当前数据库
db.dropDatabase();
(4)显示当前db的状态
db.stats();
可查看当前数据库的详细信息
(5)显示db版本
db.version();
(6)查看当前db的连接机器地址
db.getMongo();
(7)脚本支持
conntection to 127.0.0.1:27017
var i=10;
if(true){
var a=11;
}
二、集合的相关命令
1、显示创建集合
显示:db.createCollection("usersname");
步骤:
use baichi
db.createCollection("customer");//在baichi库中创建集合customer
show collections
2、隐式创建集合
db.users.insert({name:"wang3",age:60})
users代表集合名称,json串是插入的数据
注:以上说明如果没有users集合,那么Mongodb会主动创建users集合、再插入一条数据
3、显示当前数据库中的所有集合
show collections 或 show tables
4、删除集合
db.users.drop();
注:users代表要删除的集合
三、文档的相关命令(MongoDB操作文档)
1、创建文档
db.users.insert({name:"wang3",age:23})
此操作将会自动生成一个全球唯一的主键:
时间戳(生成时间)+mongo机器(mongo所在的机器)+PID(进程号)+计数器(随机产生数)
db.users.find();-------->查看刚才所插入的数据
2、删除文档
db.users.remove({}) ------>如果没有花括号,则代表删除所有数据;如果有就代表有条件删除
db.users.remove({name:"wang3"}); -------------->删除name未wang2的数据
3、修改文档
格式:db.集合名.update({},{},0,1)
第一个参数:修改的条件,json类型
第二个参数:更新的命令,更新的内容
第三个参数:如果为发现符合更新条件的数据内容,是否进行插入操作;1代表是,0代表否
第四个参数:是否进行多行更新;1代表是,0代表否
举例:
(1)db.users.update({name:"wang3"},{$set:{password:"888"}},0,1); //所有name未suns的数据设置为"888"
(2)db.users.update({},{$ins:{age:7}},0,1);//$inc为对应key中的数据进行算术运算,即在age的原有基础上统一加7岁,减法则用-7
db.users.update({name:"wang3"},{$inc:{age:2}},0,1)
4、基本查询操作
(1)查询集合中的所有内容:db.users.find()
(2)查询集合中的第一个元素:db.users.findOne();
5、条件查询
(1)"=": db.users.find({name:"sun2"}) ---->找出name是sun2的数据
(2)"!="--->$ne(not equal) db.users.find({name:{"$ne":"sun2"}}) ---》找出name不是sun2的数据
(3)">"--->$gt(great than) db.users.find({age:{"$gt":10}}) --->找出age大于10的数据
(4)"<"---->$lt(less than) db.users.find({age:{"$lt":10}}) ---->找出age小于10的数据
(5)">="-->$gte(great than equal) db.users.find({age:{"$gte":10}}) --->找出age大于等于10的数据
(6)"<="--->$lte(less than equal) db.users.find({age:{"$lte":10}})--->找出age小于等于10的数据
6、逻辑运算
(1)与运算:即“并且”;举例----》db.users.find({name:"sun2",age:10})
(2)或运算:即“或者”,关键字$or、$nor
举例:db.users.find({$or:[{name:"sun2"},{age:32}]})
db.users.find({$nor:[{name:"sun2"},{age:23}]})
使用格式:db.users.find({$nor:[{?条件}]})
7、集合运算
(1)$in类似于关系数据库中的in操作
db.users.find({age:{$in:[12,23,24]}})---->找出age在12、23、24之中的数据
(2)$all主要用于查询数组中的包含关系,查询条件中只要有一个不包含就不返回
a、{"name":"bb1","post":[1,2,3]}
b、{"name":"bb1","post":[2,3,4]}
c、{"name":"bb1","post":[2,5,7]}
d 、{"name":"bb1","post":[3,9,10]
语句:db.users.find({post:{$all:[2,3]}})}---->查询结果是a、b符合条件
8、统计、分页和排序
(1)统计
db.users.count();//所有数据条数
db.users.find().count(); //完整的条数
db.users.find({age:{$lt:5}}).count();
(2)排序
db.users.find().sort({age:1}); ----->按age升序排序
db.users.find().sort({age:-1});----->按age降序排序
(3)分页
a、db.users.find().skip(?),limit(?);
第一个参数:代表从第几条数据开始(默认是0)
第二个参数:代表每页需要几条数据
举例:
db.users.find.sort({age:-1}).skip(2).limit(3);
b、忽略分页效果,获取总条数
db.users.find().sort({age:-1}).skip(2).limit(3).count(0);
c、记录分页效果,统计当前页的数量
db.users.find().sort({age:-1}).skip(2).limit(8).count(1);
9、特殊条件查询--》投影查询
db.users.find({},{_id:0})-->第一个参数是条件,第二个参数是决定显示什么键
$exists判断一个key是否存在
db.users.find({name:{$exists:1}})
例:db.users.find({},{_id:0,age:0})-------->表示不显示id和age键
10、mongodb的索引
(1)普通索引
db.collection(集合名).ensureIndex({key(需要设置索引的键名):1})
查询索引信息:db.collection.stats();
**mongodb会自动为“_id”的键添加索引
(2)删除索引
db.collection(集合名).dropIndex({key(需要删除的索引所在的键名):1})
**删除集合也会将键中的索引删除
(2)唯一索引命令
a、创建唯一索引
db.collection.ensureIndex({{key:1},{unique:true}})
查看:db.collection.stats();
b、删除索引
db.collection.dropIndex({kay:1})
相关文章推荐
- MongoDB基本管理命令
- 数据库-mongodb-Gridfs
- 数据库-mongodb-聚合与map reduce
- 数据库-mongodb-mongod参数说明
- 数据库-mongodb-索引
- 数据库-mongodb-高级查询表达式
- 数据库-mongodb-常用命令
- mongodb安装启动mongodb
- MongoDB自动增长
- (一)MongoDB简介
- 数据库-mongodb-基础
- MongoDB固定集合(Capped Collections)
- MongoDB ObjectId
- MongoDB监控
- MongoDB分片
- MongoDB复制
- MongoDB基本管理命令操作
- windows平台安装并使用MongoDB
- MongoDB理解
- 使用Java查询MongoDB数据库并封装成查询服务实践