497 阅读2分钟

MongoDB学习笔记

json可以为我们描述多种关系:一对一、一对多、多对一和多对多

与js语法类似

创建记录会自动生成一条_id主键,唯一id,代表该Object,也可以声明为自定义的数据

{_id:"123"}

命令行

  • mongod 是用来连接到mongodb数据库服务器的,即服务器端。
  • mongo 是用来启动MongoDB shell的,是mongodb的命令行客户端。

常用操作

  • use db_name 切换db
  • show dbs 查看所有db
  • show collections 查看当前db所有collections
  • db 查看当前db

  • db.collection.insert 插入数据
  • db.collection.insertOne 插入一条数据
  • db.collection.insertMany 插入多条数据
db.people.insertOne(
   { user_id: "bcd001", age: 45, status: "A" }
)
等价:
INSERT INTO people(user_id,age,status) VALUES ("bcd001",45,"A")

  • db.collection.find() 查找
  • db.collection.findOne() 查看一个
  • db.collection.find().limit() 查找限制条数
  • db.collection.find().count() 返回找到数据条数
db.people.find(
    { status: "A" }
)
等价:SELECT * FROM people WHERE status = "A"

  • 投影

查询结果的显示哪些数据 select 投影字段 from collections

find({},{投影结果数据})

db.people.find(

{ },

{ userid: 1, status: 1, id: 0 }

)

等价:SELECT user_id, status FROM people

  • update() 替换 通过$set更换某一属性值
  • db.collection.updateOne() 更新符合条件第一条
  • db.collection.updateMany() 更新符合条件的所有
  • db.collection.replaceOne() 替换符合条件的一条(所有属性)

db.getCollection('user').update({name:"zfb"},{$set:{name:"dx",age:"12"}})

  • unset删除一个属性
  • db.collection.remove({})全删除(一个一个) 应该使用db.collections.drop(),直接删除,效率更高
  • db.collection.remove() 一个或者多个,通过设置第二个参数为true则删除一个
  • db.collection.deleteOne() 删除一条符合条件
  • db.collection. deleteMany() 删除所有符合条件
  • db.dropDatabase() 删除db,谨慎使用

db.getCollection('user').remove({name:"wgy"},true)

过滤器

  • $lt 少于
db.people.find(
   { age: { $lt: 25 } }
)
等价:
SELECT * FROM people WHERE age < 25

  • $gt 大于
db.people.find(
   { age: { $gt: 25 } }
)
等价:
SELECT * FROM people WHERE age > 25

  • $lte less than equal 小于等于
db.people.find(
   { age: { $gt: 25, $lte: 50 } }
)
等价:
SELECT * FROM people WHERE age > 25 AND   age <= 50

  • $regex /关键字/ 模糊查询
db.people.find( { user_id: /bc/ } )
等价:
db.people.find( { user_id: { $regex: /bc/ } } )
等价:
SELECT * FROM people WHERE user_id like "%bc%"

db.people.find( { user_id: /bc^/ } )
等价:
db.people.find( { user_id: { $regex: /bc^/ } } )
等价:
SELECT * FROM people WHERE user_id like "%bc"

  • $exists 存在某个属性
db.people.count( { user_id: { $exists: true } } )
等价:
SELECT COUNT(user_id) FROM people

  • $and 条件and连接
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { name: "dx"  } ] } )
等价:
select * from user where price!=1.99 and name="dx"

  • $or 条件或连接,与and对应
  • $ne 不等于某个值的记录
db.user.find( { qty: { $ne: 20 } } )
等价:
select * form user where qtf!=20

常用方法

  • explain() 解释某条sql
db.people.find( { status: "A" } ).explain()
等价:
EXPLAIN SELECT * FROM people WHERE status = "A"

  • skip() 跳过指定条数记录
db.people.find().limit(5).skip(10)
等价:
SELECT * FROM people LIMIT 5 SKIP 10

  • distinct()去重
db.people.distinct( "status" )
等价:
SELECT DISTINCT(status) FROM people

  • count()
  • limit()
  • sort() 下面索引部分

聚合操作

索引

排序

索引可以用于排序sort()

1升 -1降

find().sort({user_id:1})根据user_id升序排序

创建索引

db.collection.createIndex( { name: -1 } )

Diagram of a query that uses an index to select and return sorted results. The index stores ``score`` values in ascending order. MongoDB can traverse the index in either ascending or descending order to return sorted results.

SpringBoot—MongoTemplate

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>