##用"net start/stop MongoDB"启动服务时,要要以管理员的身份运行cdm,否则无法找到服务
MongoDB数据库是以集合为单位的
- use 数据库 不是集合
mongodb插入数据
- db.collection.insert({}) 插入数据,id存在就出错
- dbcollection.save({}) 插入数据,_id存在就修改,_id不存在就添加
###修改数据 db.集合名称。update(,,{multi:})
-
db.collection.update({“name”:“hr”},{“name”:“mnc”}) 更新一条,根据键值对更新
-
db.collection.update({“name”:“hr”},{$set:{“name”:“mnc”}}) 更新一条
-
db.collection.update({},$set:{“gender”:0},{multi:true}) muiti参数默认为false时,只更新一条数据,为true时,更新全部数据
###删除数据库(先use db到指定数据库 )
- db.dropDatabase()
{ “dropped” : “runoob”, “ok” : 1 }
####创建集合 - db.createCollection(“name”)
###删除集合 - db.collection.drop()
###删除数据
-db.collection.remove({“age”:“22”},{justone:true}) justone 默认为false,即删除全部指定条件的数据。为false时,只删除一条数据
条件查询
-
db.collection.find({“age”:“22”}) 通过某条数据中的片段(键值对)来进行限定查询,这是一个多行查询
-
db.collection.findOne({“age”:“22”}) 只查询符合条件的一条数据
-
db.collection.find({“age”:“22”}).pretty() 格式化输出查询结果
####比较运算符
-
等于: :
-
小于: $lt(less than)
-
小于等于: $lte(less than equal)
-
大于: $gt(greater than)
-
大于等于: $gte(greater than equal)
-
不等于: $ne
-
范围: $in
-
db.collection.find({“age”:{$lt:18}}) 查询年龄小于18的所有的数据
-
db.collection.find({age:{$in:[18,28,38]}}) 查询年龄18,28,38 的数据
-
db.collection.find({“age”:“18”,“addr”:“上海”})多字段查询
逻辑运算符
-
and :在json中写多个条件即可
-
$or :值为数组,数组中每个元素为json
db.test1000.find({$or:[{age:12},{"name":"xiaohong"}]})
# $or:[{键值对},键值对]
-
$nor: (既不也不)
-
db.collection1.find({$nor:[{name:“xiaomu”},{name:“xiaoli”}]}) 查询姓名不为xiaomu,xiaoli的全部数据
-
不在范围: $nin:[]
正则表达式
db.collection.find({name:/^abc/}) #name 字段中包含abc的所欲数据
db.test1000.find({name:{$regex:"hong"}})
#包含hong这个字段的数据
db.test1000.find({name:{$regex:"hong$"}}) #以hong结尾的字段的数据
###分页:
- db.collection1.find().skip(0).limit(10) ;//查询从0条开始,查询10条,skip()方法默认参数为 0 ,表示跳过的条数 (当数据量大的时候,先skip再limit)
###自定义查询
- db.test1000.find({$where:function(){return this.age>18;}}) 查询年龄大于18的数据
###格式化输出
- db.collection.find({},_id:0,name:1).pretty()
###投影查询
- db.collection.find({},_{id:0,name:1}) 参数为1表示显示,参数为0为不显示,如果不显示字段,要明确设置为0
###排序
- db.collection1.find().sort({age:1});//按照年龄升序排序,为-1的话表示降序排序
###存在
- db.c2.find({hight:{$exists:1}});//查询存在hight字段的数据
###查看数据总条数
- db.collection1.count();//统计集合collection1中的数据数量
- db.collection1.find({条件}).count();//统计集合collection1中的数据数量
#消除重复
- db.collection.distinct({})
##数据备份与恢复
-
备份:mongodump -h dbhost -d dbname -o dbdirectory
-
-h:mongodb所在服务器地址,如127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
-
-d:需要备份的数据库实例,如:test
-
-o:需要备份的数据存放的位置,例如:d:\data (如果没有则自动创建)
- 恢复:mongorestore -h dbhost -d dbname -directoryperdb dbdirectory
-
-h:mongodb所在服务器地址,如127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
-
-d:需要备份的数据库实例,如:test,此名称也可以和备份的时候不一样
-
-directoryperdb :备份数据所在的位置,例如:d:\data
注意:恢复时要在退出mongo命令行状态下执行
###导入和导出
- 导出:mongoexport -h dbhost -d dbname -c collectionName -o output
- -h:数据库地址
- -d:指明使用的库
- -c:指明要导出的集合
- -o:指明要导出的文件路径加文件名,文件名可以是txt、word、excel等多种文件格式
-导入:mongoimport -h dbhost -d dbname -c collectionname 文件的地址路径
- -h:数据库地址
- -d:指明使用的库
- -c:指明要导出的集合
- 注意:导入时要在退出mongo命令行状态下执行,导入导出与数据备份和恢复相似
mongodb聚合命令
db.collection.aggregate(
{ $group:{
_id:"$gender", #_id:(可以是多个参数,后续调用时,用.对象名的方法)
counter:{$sum:1}
}
}
)
其中还有
- $match(匹配到适合条件的数据)
- $project:(投影显示)修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
- $min,max,sun,avg,first,last 等
- $limit:用来限制MongoDB聚合管道返回的文档数。
- $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
- $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。(有一个保留空值和空列表的参数:perserve…)
- $sort:将输入文档排序后输出。
- $geoNear:输出接近某一地理位置的有序文档。