mongodb基础入门(安装、可视化、用户、增删改查)

安装

  1. 安装教程参考https://blog.csdn.net/heshushun/article/details/77776706
  2. 安装卡住参考https://blog.csdn.net/Artful_Dodger/article/details/80844844
  3. 安装可视化工具参考https://blog.csdn.net/qq_34731574/article/details/81428835
    百度网盘失效请到官网安装https://robomongo.org/download
    lyt_angularjs
  4. 用户、角色创建参考https://www.cnblogs.com/zhoujinyi/p/4610050.html

数据库、集合操作

  1. 查看所有数据库列表
    show dbs
  2. 使用|创建数据库
    use 数据库名称
  3. 查看数据库中集合
    show collections
  4. 使用|创建集合student为集合名称
    db.student.insert({name:“lyt”,age:20});
  5. 删除当前数据库
    db.dropDatabase()
  6. 删除集合
    db.student.drop()

集合增

db.student.insert({name:“lyt”,age:20});
db.student.insertMany([{name:“lyt”,age:20},{name:“lyt1”,age:21}]);

集合查

student为集合名称

  1. 查询所有记录
    db.student.find()
    mysql:select * from student
  2. 查询 age = 22 的记录
    db.student.find({age: 22})
    mysql:select * from student where age = 22
  3. 查询 age > 22 的记录
    db.student.find({age:{$gt:22}})
    mysql:select * from student where age>22
  4. 查询 age < 22 的记录
    db.student.find({age:{$lt:22}})
    mysql:select * from student where age<22
  5. 查询 age >= 25 的记录
    db.student.find({age: {$gte: 25}})
    mysql:select * from student where age >= 25
  6. 查询 age <= 25 的记录
    db.student.find({age: {$lte: 25}})
    mysql:select * from student where age<=25
  7. 查询 age >= 23 并且 age <= 26
    db.student.find({age:{$ gte:23,$lte:26}})
    mysql:select * from student where age>=23 and age<=26
  8. 查询 name 中包含 mongo 的数据 模糊查询用于搜索
    db.student.find({name:/mongo/})
    mysql:select * from student where name like ‘%mongo%’
  9. 查询 name 中以 mongo 开头的
    db.student.find({name:/^mongo/})
    mysql:select * from student where name like ‘mongo%’
  10. 查询指定列 name、age 数据
    db.student.find({},{name:1,age:1})
    mysql:select name,age from student
    name 也可以用 true|false,name:ture和name:1 效果一样,如果用 false 就是排除 name,显示 name 以外的列信息。
  11. 查询指定列 name、age 数据, age > 25
    db.student.find({age:{$gt:25}},{name:1,age:1})
    mysql:select name,age from student where age>25
  12. 按照年龄排序 1 升序 -1 降序
    db.student.find().sort({age:1})
    db.student.find().sort({age:-1})
    mysql:select * from student order by age
    mysql:select * from student order by age desc
  13. 查询 name = zhangsan, age = 22 的
    db.student.find({name:‘zhangsan’,age:22})
    mysql:select * from student where name=‘zhangsan’ and age=22
  14. 查询前 5 条
    db.student.find().limit(5)
    mysql:select * from student limit 5
  15. 查询 10 条以后的数据
    db.student.find().skip(10)
    mysql id自增:select * from student where id>(select max(id) from student limit 10)
    mysql:select * from student where id not in(select * from student limit 10)
  16. 查询在 5-15 之间的数据
    db.student.find().skip(5).limit(10)
    mysql:select * from student limit 5,10
    可用于分页,skip 是第几页*pageSize,limit 是 pageSize
  17. or 与 查询
    db.student.find({$or:[{age:22},{age:25}]})
    mysql:select * from student where age=22 or age=25
  18. findOne查询第一条数据
    db.student.findOne()
    db.student.find().limit(1)
    mysql:select * from student limit 1
  19. 查询某个结果集的记录条数 统计数量
    db.student.find(age:{$gte:25}).count()
    mysql:select count(*) from student where age>=25
    如果要返回限制之后的记录数量,要使用 count(true)或者 count(非 0)
    db.users.find().skip(10).limit(5).count(true)

集合改

  1. 查找名字叫做小明的,把年龄更改为 16 岁
只改变第一条匹配的 db.student.update({name:'xm'},{$set:{age:13}}) 
mysql:update student set age=13 where name='xm' limit 1
更改所有匹配项目  db.student.update({name:'xm'},{$set:{age:13},multi:true})
mysql:update student set age=13 where name='xm'
  1. 查找数学成绩是 70,把年龄更改为 33 岁
    db.student.update({“score.shuxue”:70},{$set:{“age”:33}})
    mysql:
  2. 完整替换,不出现$set 关键字了: 注意
    db.student.update({name:‘lyt’},{name:‘aaa’,age:23})
  3. 某字段数值增加
    db.student.update({name:‘lyt’},{KaTeX parse error: Expected 'EOF', got '}' at position 13: inc:{age:32}}̲,false,true) my…inc: {age: 50}, $set: {name: ‘aaa’}}, false, true)
    mysql:update users set age = age + 50, name =‘aaa’ where name =‘lyt’

集合删

  1. 删除第一条符合条件的
    db.student.remove({name:‘lyt’},{justOne:true})
    mysql:delete from student limit 1
  2. 删除所有符合条件的
    db.student.remove({name:‘lyt’})
    mysql:delete from student

索引

  1. 创建索引
    db.student.ensureIndex({name:1})
  2. 获取当前集合的索引
    db.student.getIndexes()
  3. 删除索引
    db.student.dropIndex({name:1})
  4. 复合索引,1升-1降
    db.student.ensureIndex({name:1,age:-1})
    注意:该索引被创建后,基于 name 和 age 的查询将会用到该索引,或者是基于 name 的查询也会用到该索引,但是只是基于 age 的查询将不会用到该复合索引。因此可以说, 如果想用到复合索引,必须在查询条件中包含复合索引中的前 N 个索引列。然而如果查询 条件中的键值顺序和复合索引中的创建顺序不一致的话,MongoDB 可以智能的帮助我们调 整该顺序,以便使复合索引可以为查询所用。
    对于上面创建的索引,MongoDB 都会根据索引的 keyname 和索引方向为新创建的索引
    自动分配一个索引名,下面的命令可以在创建索引时为其指定索引名
    db.student.ensureIndex({“username”:1},{“name”:“userindex”})
    注意:随着集合的增长,需要针对查询中大量的排序做索引。如果没有对索引的键调用 sort, MongoDB 需要将所有数据提取到内存并排序。因此在做无索引排序时,如果数据量过大以 致无法在内存中进行排序,此时 MongoDB 将会报错
    5.唯一索引
    db.user.ensureIndex({userId:1},{unique:true})

连接mongdb

  1. 无账户密码
    const url=“mongodb://localhost:27017/testdb”;
  2. 有账户密码
    const url=“mongodb://admin:123456@localhost:27017/testdb”
发布了38 篇原创文章 · 获赞 66 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/lyt_angularjs/article/details/87799114
今日推荐