【数据库】MongoDB

发布于:2024-04-27 ⋅ 阅读:(21) ⋅ 点赞:(0)

因上努力

个人主页:丷从心·

系列专栏:MongoDB

学习指南:数据库

果上随缘


数据库操作

查询数据库
show dbs
切换数据库
  • MongoDB中无需手动创建数据库,当切换到一个不存在的数据库时,会自动创建一个数据库
use student_info
查询当前数据库
db
删除数据库
use student_info

db.dropDatabase()
查询数据库版本
db.version()

数据集合操作

创建数据集合
db.createCollection('class_1')
  • 也可以不用手动创建数据集合,当使用一个不存在的数据集合时,会自动创建一个数据集合
查询数据集合
show collections
show tables
删除数据集合
db.class_1.drop()

数据插入

use student_info

db.class_1.insertOne({'name': 'follow__my_heart', 'age': 22})
  • 数据查询
db.class_1.find()
插入id重复的数据
db.class_1.insertOne({_id: 10010, 'name': 'follow__my_heart', 'age': 22})

db.class_1.save({_id: 10010, 'name': 'follow__my_heart', 'age': 18})

数据更新

数据更新一条
丢失其他字段
db.class_1.update({'name': 'follow__my_heart'}, {'name': 'follow__your_heart'})
保留其他字段
db.class_1.update({'name': 'follow__my_heart'}, {$set: {'name': 'follow__your_heart'}})
数据批量更新
db.class_1.update({'name': 'follow__my_heart'}, {$set: {'name': 'follow__your_heart'}}, {multi: true})

数据删除

数据删除一条
db.class_1.remove({'name': 'follow__your_heart'}, {justOne: true})
数据批量删除
db.class_1.remove({'name': 'follow__my_heart'})

数据查询

创建数据集合
use book_manager

db.getCollection('person_info').insert({
    name: '郭靖',
    hometown: '蒙古',
    age: 20,
    gender: true
})
db.getCollection('person_info').insert({
    name: '黄蓉',
    hometown: '桃花岛',
    age: 18,
    gender: false
})
db.getCollection('person_info').insert({
    name: '华筝',
    hometown: '蒙古',
    age: 18,
    gender: false
})
db.getCollection('person_info').insert({
    name: '黄药师',
    hometown: '桃花岛',
    age: 40,
    gender: true
})
db.getCollection('person_info').insert({
    name: '段誉',
    hometown: '大理',
    age: 16,
    gender: true
})
db.getCollection('person_info').insert({
    name: '段王爷',
    hometown: '大理',
    age: 45,
    gender: true
})
db.getCollection('person_info').insert({
    name: '洪七公',
    hometown: '华山',
    age: 18,
    gender: true
})

use book_shop

db.getCollection('product_info').insert({
    _id: 100,
    sku: 'abc123',
    description: 'Single line description'
})
db.getCollection('product_info').insert({
    _id: 101,
    sku: 'abc456',
    description: 'First line\nSecond line'
})
db.getCollection('product_info').insert({
    _id: 102,
    sku: 'abc789',
    description: 'Single line description'
})
db.getCollection('product_info').insert({
    _id: 103,
    sku: 'xyz123',
    description: 'Many lines description'
})
db.getCollection('product_info').insert({
    _id: 104,
    sku: 'xyz456',
    description: 'Many lines description'
})
数据查询一条
db.person_info.findOne({'age': 18})
查询格式化输出
  • 在终端中使用
db.person_info.find().pretty()
运算符
比较运算符
db.person_info.find({age: {$gte: 18}})
范围运算符
db.person_info.find({age: {$in: [18, 45]}})
逻辑运算符
db.person_info.find({$or: [{'age': 18}, {'hometown': '桃花岛'}]})
正则表达式查询
db.product_info.find({sku: /^abc/})
db.product_info.find({sku: {$regex: '^abc'}})
分页
查询前两条数据
db.product_info.find().limit(2)
略过前两条数据
db.product_info.find().skip(2)
自定义查询
db.person_info.find({
    $where: function () {
        return this.age <= 18;
    }
})
投影
db.person_info.find({age: {$gte: 18}}, {'name': 1, _id: 0})
排序
db.person_info.find().sort({'age': -1})
统计
db.person_info.count({'age': {$gte: 18}})
去重
db.person_info.distinct('hometown', {'age': {$gte: 18}})
聚合操作
$group
按指定字段分组
db.person_info.aggregate({
    $group: {_id: '$gender', count: {$sum: 1}, avg_age: {$avg: '$age'}}
})
  • {$sum: 1}中的1表示倍数
不进行分组
db.person_info.aggregate({
    $group: {_id: null, count: {$sum: 1}, avg_age: {$avg: '$age'}}
})
$project
投影
db.person_info.aggregate({
    $project: {_id: 0, name: 1, age: 1}
})
别名
db.person_info.aggregate(
    {$group: {_id: '$gender', count: {$sum: 1}, avg_age: {$avg: '$age'}}},
    {$project: {"性别": '$_id', "人数": '$count', "平均年龄": '$avg_age', _id: 0}}
)
$match
db.person_info.aggregate(
    {$match: {age: {$gt: 20}}},
    {$group: {_id: '$gender', count: {$sum: 1}}},
    {$project: {"性别": '$_id', "人数": '$count', _id: 0}}
)
$sort
db.person_info.aggregate(
  {$group: {_id: '$gender', count: {$sum: 1}}},
  {$sort: {count: -1}}
)
$limit
db.person_info.aggregate(
    {$limit: 2}
)
$skip
db.person_info.aggregate(
    {$skip: 2}
)


网站公告

今日签到

点亮在社区的每一天
去签到