MongoDB

(一)基本命令

  • 显示当前数据库服务器上的数据库

show dbs

  • 切换到指定数据库pagedb的,可以在此管理pagedb数据库以及其中的集合等

use pagedb

  • 显示数据库中的所有集合

show collections

  • 查看数据库服务器状态

db.serverStatus()

  • 查询指定数据库统计信息

db.status()

  • 创建数据 库 mydatabase

use mydatabase;(只是切换,不是创建)

  • 创建集合 student

db.createCollection("student",{capped:ture,size:1000,max:100})

  1. db.student.save({_id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]})
  2. db.student.save({_id:2,classid:2,age:19,name:"little2",love:["play"]})
  3. db.student.save({_id:3,classid:2,age:20,name:"little3"})
  4. db.student.save({_id:4,classid:1,age:21,name:"little4"})
  5. db.student.save({_id:5,classid:1,age:22,name:"little5",opt:"woshisheia"})
  6. db.student.save({_id:6,classid:2,age:23,name:"23little4"})
  7. 或者
  8. db.student.insert([
  9. {_id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]},
  10. {_id:2,classid:2,age:19,name:"little2",love:["play"]},
  11. {_id:3,classid:2,age:20,name:"little3"},
  12. {_id:4,classid:1,age:21,name:"little4"},
  13. {_id:5,classid:1,age:22,name:"little5",opt:"woshisheia"},
  14. {_id:6,classid:2,age:23,name:"23little4"},
  15. ])
  • 查询一条记录/多条记录(查询出name为little的数据)

db.student.find({name:"little1"})

  • 从第二条查寻,查出第三条

db.student.find().skip(1).limit(3)

  • 查询出19

db.student.find({age:{gt:19,lte:21}})

=""查询出age为奇数的数据(对2求余为1即为奇数)=""$mod=""

db.student.find({age:{$mod:[2,1]}})

="" 查询出存在opt字段的数据="" [exists](https://www.bookstack.cn/read/piaosanlang-mongodb/=%22%22%20docs.mongodb.com=%22%22%20manual=%22%22%20reference=%22%22%20operator=%22%22%20query=%22%22%20exists=%22%22 "exists")=""

db.student.find({opt:{$exists:true}})

="" 查询出不存在opt字段的数据=""

db.student.find({opt:{$exists:false}})

="" 查询出name不为little2的数据="" [ne](https://www.bookstack.cn/read/piaosanlang-mongodb/=%22%22%20docs.mongodb.com=%22%22%20manual=%22%22%20reference=%22%22%20operator=%22%22%20query=%22%22%20ne=%22%22 "ne")=""

db.student.find({name:{$ne:"little2"}})

="" 查询出age为16,18,19的数据="" [in](https://www.bookstack.cn/read/piaosanlang-mongodb/=%22%22%20docs.mongodb.com=%22%22%20manual=%22%22%20reference=%22%22%20operator=%22%22%20query=%22%22%20in=%22%22 "in")=""

db.student.find({age:{$in:[16,18,19]}})

="" 查询出age不为16,18,19的数据="" [nin](https://www.bookstack.cn/read/piaosanlang-mongodb/=%22%22%20docs.mongodb.com=%22%22%20manual=%22%22%20reference=%22%22%20operator=%22%22%20query=%22%22%20nin=%22%22 "nin")=""

db.student.find({age:{$nin:[16,18,19]}})

="" 查询出love有三个的数据="" $size=""

db.student.find({love:{$size:3}})

="" 查询出name不是以litt开头的数据="" [not](https://www.bookstack.cn/read/piaosanlang-mongodb/=%22%22%20docs.mongodb.com=%22%22%20manual=%22%22%20reference=%22%22%20operator=%22%22%20query=%22%22%20not=%22%22 "not")[regex](https://www.bookstack.cn/read/piaosanlang-mongodb/=%22%22%20docs.mongodb.com=%22%22%20manual=%22%22%20reference=%22%22%20operator=%22%22%20query=%22%22%20regex=%22%22 "regex")=""

db.student.find({name:{$not:/^litt.*/}})

="" 查询age="">20的数据

db.student.find({age:{$gt:20}})

按age升序

db.student.find().sort({age:1})

按age降序

db.student.find().sort({age:-1})

查询数据条数

db.student.find().count()

  • 删除记录

    删除age为20的数据

db.student.remove({age:20});

  • 创建索引

    创建索引 age

db.student.ensureIndex({"age":1})

  • 查询索引

db.student.getIndexes()

  • 删除索引

db.student.dropIndex({"age" : 1})

  • 统计集合记录数

db.student.count()

  • 删除集合 student

db.student.drop()

  • 删除数据库 mydatabase

db.dropDatabase()
(三)安全管理

  • 以安全认证模式启动

  • 添加用户

  • 安全认证

(四)数据备份、恢复与迁移管理
  • 备份全部数据库

  • 备份指定数据库

  • 备份一个数据库中的某个集合

  • 恢复全部数据库

  • 恢复某个数据库的数据

  • 恢复某个数据库的某个集合的数据

  • 向MongoDB导入数据

  • 从MongoDB导出数据

(五)远程连接管理

基于mongo实现远程连接

mongo ---host 192.168.17.129 ---port 27017

(六)聚合
  • 计算各班级平均年龄
复制代码
  1. db.student.aggregate([
  2. {
  3. $group: {
  4. _id: "$classid",
  5. avg_age: {
  6. $avg: "$age"
  7. }
  8. }
  9. }
  10. ]
  11. )
  • 计算全班级平均年龄
复制代码
  1. db.student.aggregate([
  2. {
  3. $group: {
  4. _id: null,
  5. avg_age: {
  6. $sum: "$age"
  7. }
  8. }
  9. }
  10. ]
  11. )
(七) MapReduce
  • 计算各班级平均年龄

分组统计(MapReduce):

  1. var mapfun = function(){emit(this.classid,this.age);}
  2. var reducefun = function(key, values){
  3. var total=0;
  4. for(var i=0;i<values.length;i++){
  5. total += values[i];
  6. }
  7. return { classid: key, age_avg: total/values.length };
  8. }
  9. db.student.mapReduce(
  10. mapfun,
  11. reducefun,
  12. { out: "student_res" }
  13. )

查询统计结果(分班级统计人数)

db.student_res.find()

  • 各班级统计人数

分组统计(MapReduce):

复制代码
查询统计结果(分班级统计人数)
  1. var mapfun = function(){emit(this.classid,1);}
  2. var reducefun = function(key, values){
  3. return Array.sum(values);
  4. }
  5. db.student.mapReduce(
  6. mapfun,
  7. reducefun,
  8. { out: "student_sta" }
  9. )

db.student_sta.find();

相关推荐
总有刁民想爱朕ha4 分钟前
Windows Server 2019部署MySQL 8教程
数据库·windows·mysql
纪伊路上盛名在6 分钟前
记1次BioPython Entrez模块Elink的debug
前端·数据库·python·debug·工具开发
程序员水自流9 分钟前
MySQL数据库自带系统数据库功能介绍
java·数据库·mysql·oracle
旧梦吟12 分钟前
脚本网页 三人四字棋
前端·数据库·算法·css3·html5
小光学长17 分钟前
基于ssm的考研复习平台w0ws1848(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
-大头.20 分钟前
数据库高可用架构终极指南
数据库·架构
Elastic 中国社区官方博客23 分钟前
Elasticsearch:构建一个 AI 驱动的电子邮件钓鱼检测
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
德育处主任Pro29 分钟前
在亚马逊云上解决RDS、MariaDB 与 Aurora MySQL复制延迟实战指南
数据库·mysql·mariadb
l1t34 分钟前
解决PostgreSQL中找不到uniq函数的错误
数据库·postgresql
墨白曦煜41 分钟前
深入剖析 Redis 客户端:Sentinel 模式下的“寻址”与“感知”艺术
数据库·redis·sentinel