【数据库】MongoDB

server/2024/10/20 9:53:44/

文章目录

    • @[toc]
      • 数据库操作
      • 数据集合操作
        • 创建数据集合
        • 查询数据集合
        • 删除数据集合
      • 数据插入
        • 插入id重复的数据
      • 数据更新
        • 数据更新一条
          • 丢失其他字段
          • 保留其他字段
        • 数据批量更新
      • 数据删除
        • 数据删除一条
        • 数据批量删除
      • 数据查询
        • 创建数据集合
        • 数据查询一条
        • 查询格式化输出
        • 运算符
          • 比较运算符
          • 范围运算符
          • 逻辑运算符
        • 正则表达式查询
        • 分页
          • 查询前两条数据
          • 略过前两条数据
        • 自定义查询
        • 投影
        • 排序
        • 统计
        • 去重
        • 聚合操作
          • $group
            • 按指定字段分组
            • 不进行分组
          • $project
            • 投影
            • 别名
          • $match
          • $sort
          • $limit
          • $skip

因上努力

个人主页:丷从心·

系列专栏:MongoDB

学习指南:数据库

果上随缘


数据库操作

查询数据库
show dbs
切换数据库
use student_info
查询当前数据库
db
删除数据库
use student_infodb.dropDatabase()
查询数据库版本
db.version()

数据集合操作

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

数据插入

use student_infodb.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_managerdb.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_shopdb.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}
)


http://www.ppmy.cn/server/18152.html

相关文章

Uniapp 点击图片放大

1、html(循环图片) <view v-for"(i,index) in photo_list" :key"photoindex"><img :src"i" alt"" click"ClickImage(photo_list,i)" /></view> 2、js(方法) ClickImage(PhotoAddress, index) {uni.previ…

【c++】vector模拟实现与深度剖析

&#x1f525;个人主页&#xff1a;Quitecoder &#x1f525;专栏&#xff1a;c笔记仓 vector涉及到许多细节问题&#xff0c;比如双层深拷贝&#xff0c;迭代器失效等&#xff0c;本篇文章我们通过模拟实现来深度理解这块的内容 目录 1.基本框架2.构造和销毁3.元素访问4.获取…

JavaEE——Spring Boot + jwt

目录 什么是Spring Boot jwt&#xff1f; 如何实现Spring Boot jwt&#xff1a; 1. 添加依赖 2、创建JWT工具类 3. 定义认证逻辑 4. 添加过滤器 5、 http请求测试 什么是Spring Boot jwt&#xff1f; Spring Boot和JWT&#xff08;JSON Web Token&#xff09;是一对常…

【Ajax-异步刷新技术】什么是Ajax之续章 !

文章目录 Ajax第五章1、layui的后台布局2、layui的数据表格1、在jsp页面中编写table2、在页面中引入文件3、编写代码4、参照文档修改表格属性 **3、最终效果** 第六章1、继续第五章内容1、layui组件2、添加数据3、查看数据4、修改数据5、删除数据 2、批量删除核心 3、数据表格重…

Qt : 禁用控件默认的鼠标滚轮事件

最近在写一个模拟器&#xff0c;在item中添加了很多的控件&#xff0c;这些控件默认是支持鼠标滚动事件的。在数据量特别大的时候&#xff0c;及容易不小心就把数据给修改了而不自知。所有&#xff0c;我们这里需要禁用掉这些控件的鼠标滚轮事件。 实现的思想很简单&#xff0c…

2014NOIP普及组真题 1. 珠心算测验

线上OJ&#xff1a; 一本通&#xff1a;http://ybt.ssoier.cn:8088/problem_show.php?pid1965 核心思想&#xff1a; 1、题目所求为“有多少个数其他两个数之和”&#xff0c;故不管5是由14组成&#xff0c;还是23组成&#xff0c;都只算一次。 2、利用 set 有 自动去重 的功…

【U+】U+智享版运维平台账号密码重置

【问题描述】 友加畅捷系列中的U智享版软件&#xff0c; 系统运维平台账号admin密码忘记了&#xff0c;无法登录。 【解决方法】 在软件的安装目录下&#xff0c;找到sysconfig_accounts文件&#xff0c;并删除。 【路径&#xff1a;X:\U系列软件\U智享版\WebSite\config\】 …

electron中preload.js文件的用法

在Electron中&#xff0c;preload.js文件扮演着非常重要的角色&#xff0c;它允许你在渲染进程中的全局作用域里安全地、有选择地集成Node.js和Electron的API。这是一种在保持渲染进程与主进程隔离的同时&#xff0c;又能使渲染进程具有访问特定Electron API的能力的方法。这种…