美文网首页
MongoDB(1)

MongoDB(1)

作者: royluck | 来源:发表于2021-06-13 11:52 被阅读0次
db.shsxt.insert({
  "name": "roy"
})
db.shsxt.find()
db.dropDatabase()
db.createCollection("shsxt") // 创建集合(表)
// MongoDB不需要创建集合,在创建文档时会自动创建集合
db.table1.insert({"a":1}) # 当第一个文档插入时,集合就会被创建并包含该文档
db.table2 # 创建一个空集合
db.shsxt.drop() # 删除集合
image.png
  • 操作集合(表)

注:表和库最好不要重名,因为你删表会同时删了库;

  • 操作文档(row/行)
insert()
save()
insertMany() 
  • 更新文档
update() // 等价于updateOne()
updateOne()
save() // 更新必须指定id
db.user.update(
{}, // 所有集合
{"$inc"}: { "age": -1}, // age字段减1
{"multi": true}
)
  • 删除文档
remove() // 移除集合中的数据
deleteOne() // 删除符合条件的第一个文档
db.user.remove({}) // 等价于 db.user.deleteMany({})
  • 查询
db.user.find()
db.user.distinct('name') // 去重
db.user.find().pretty() // 以格式化的方式来显示所有文档
  • 比较运算
= 
!= ('$ne')
> ('$gt')
< ('$lt')
>= ('$gte')
<= ('$lte')
image.png
  • 逻辑运算
$and
$or
$not
image.png
image.png
image.png
image.png
  • 成员运算
$in 
$nin
image.png
  • 操作符


    image.png
    image.png
  • 正则
image.png
  • 投影


    image.png
  • 数组

db.user.find({
"hobbies": { $all: ["read", "music"] }
})
image.png
image.png
image.png
嵌入文档
  • 排序


    image.png
  • 分页


    image.png
    分页公式
  • 统计


    image.png

  • 聚合

相关文章

网友评论

      本文标题:MongoDB(1)

      本文链接:https://www.haomeiwen.com/subject/hkuueltx.html