一、MongoDB是什么
MongoDB是一种NoSql类型的数据库,没有固定的表结构,是以文档的形式来存储数据的
二、MongoDB的安装
1、window下mongo的安装
(1)下载window 64位版,因为32位版的只支持最大2g的数据所以建议使用64位版本的
http://www.mongodb.org/downloads
(2)安装
- 配置环境变量
- cmd中配置mongodb
必须配置log文件和data文件夹,log是 mongo的日志文件,data存放的数据库中数据
命令是:mongod -logpath E:\JAVA\mongodb\log\mongo.log -dbpath E:\JAVA\mongo\data
配置mongo为window的服务的命令是:E:\JAVA\mongodb>mongod -logpath E:\JAVA\mongodb\log\mongo.log -logappend -dbpath E:\JAVA\mongodb\data -directoryperdb -serviceName MongoDB -install
安装服务的时候data和log的路径只能是绝对路径,不能是相对路径
-logpath 日志文件
-dbpath 数据库
-logappend 将日志写到一个文件中
--directoryperdb的使用场景就是我们有不同的物理磁盘,每个db的大小能够很好的放在相应的磁盘上的时候,可以通过这种配置方式提高访问磁盘的速度。
-install 安装为window的service
2、linux下的mongo的安装(暂未使用)
三、mongo的使用
1、使用mongo命令开启mongo的连接
2.、mongo的语法
document的结构:json形式的数据存储
(1)查找:find的使用
- 查找操作:
db.collection.find({查询条件键值对},{需要查询出来的列})
查找出来的列中值为1或者true的显示包含列为0或者false的话就是不包含列
Find返回的是游标,使用hasNext和next进行遍历
Find().sort({field:1})按照field排序
Find().skip(num)跳过num行,移动游标
Find().limit(num)分页相当于mysql中的limit
游标方法forEach可以遍历游标
查询语句:find 比较条件的符号都在内层的文档修改器都在外层文档且只能用一个
查询条件符号:$lt $lte $gt $gte < <= > >= $ne != 不等于 $in相当于sql的in
$nin 相当于sql中的not in
db.mytest.find({age:{$in:[77,78,79]}})
$or相当于sql中的or
db.mytest.find({$or:[{x:1},{age:77}]})
$mod 取余
db.mytest.find({_id:{$mod:[3,1]}}) 数组第一个数为除的数,第二个数为余数,当满足为余数的文档被查出来
$not 相当于sql中的not
db.mytest.find({_id:{$not:{$mod:[3,1]}}}) 得到id不为除三余1的记录
null值
db.mytest.find({cc:null}) 查询将会匹配所有的cc:null和所有的没有cc这个键的记录
db.mytest.find({cc:{$in:[null],$exists:true}})查询存在cc这个键值,同时cc为null的记录
正则表达式查询:
db.mytest.find({name:/zhang/}) 类似于like
db.mytest.find({name:/zhang?/i}) 忽略大小写
数组查询:
db.mytest.insert({"test":["apple","banana","peach"]})
db.mytest.find({test:"apple"}) 通过数组名直接查询
$all 查询数组多个数组值查询
db.mytest.find({test:{$all:["apple","test"]}})
通过下标查数组
db.mytest.insert({"fruit":["apple","watermelon","test"]})
db.mytest.find({"fruit.0":"apple"})
$size 查询指定长度的数组
db.mytest.find({"fruit":{$size:3}})
$slice 返回数组的一个子集合
db.testData.find({},{comment:{$slice:1}})$slice 1 第一条 -1倒数第一条
db.testData.find({},{comment:{$slice:[2,2]}})跳过前2条,显示第三条,第四条
查询内嵌文档
(1) 使用点加上子文档属性的方式
db.testData.find({"comments.name":"joe","comments.score":{$gt:10}})
(2) 使用$elemMatch匹配
db.testData.find({"comments":{"$elemMatch":{"name":"marry","score":{$gt:30}}}})
查询的文档结构是
{ "_id" : ObjectId("54475beadfdc0afaff2d1374"), "title" : "a big log", "content" : "test tstst", "comments" : [ { "name" : "joe", "email" : "test@qq.com", "content" : "nice comment", "score" : 20 }, { "name" : "marry", "email" : "mary@qq.com", "content" : "nice comment", "score" : 40 } ] } |
$where查询 效率慢
db.testData.insert({"a":1,"b":2,"c":2})
查询有相同值得文档记录
db.testData.find({$where:function(){
for(var curr in this){
for(var other in this){
if(curr != other && this[curr]== this[other]){
return true;
}
}
}
return false;
}})
高级选项
$snapshot 使用快照 boolean
$maxscan 最多扫描的文档数量 Integer
$min 查询的开始条件 document
$max 查询的结束条件 document
$hit 指定服务器用哪个索引搜寻 document
$explain 获取查询执行的细节 Boolean
使用的方法
find方法发送到服务器其实就是发送$query{}到服务器
db.testData.find({$query:{"a":1},$snapshot:true})
- 插入操作 insert