【自学笔记】MongoDB基础知识点总览-持续更新

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


MongoDB基础知识点总览

目录

  1. MongoDB简介
  2. 安装与配置
  3. 基本概念
  4. CRUD操作
  5. 索引
  6. 聚合管道
  7. 复制与分片

1. MongoDB简介

MongoDB是一个面向文档的NoSQL数据库,它使用JSON风格的文档来存储数据。它提供了高性能、高可用性和可扩展性。

示例代码:连接MongoDB

javascript 复制代码
const { MongoClient } = require('mongodb');

const uri = "your_mongodb_uri";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

async function run() {
    try {
        await client.connect();
        console.log("Connected successfully to server");
        const database = client.db('your_database_name');
        console.log("Connected correctly to database");
    } finally {
        await client.close();
    }
}

run().catch(console.dir);

2. 安装与配置

安装MongoDB

  • 在Linux上安装 :使用包管理器(如aptyum)。
  • 在Windows上安装:从MongoDB官网下载并安装。
  • 在macOS上安装:使用Homebrew。

配置MongoDB

  • 配置文件路径:/etc/mongod.conf(Linux),mongod.cfg(Windows)。
  • 启动MongoDB服务:sudo systemctl start mongod(Linux),mongod --config <path_to_config>(手动)。

3. 基本概念

  • 文档(Document):MongoDB中的基本数据单元,类似于JSON对象。
  • 集合(Collection):一组文档的集合。
  • 数据库(Database):一个或多个集合的集合。

示例代码:创建数据库和集合

javascript 复制代码
const { MongoClient } = require('mongodb');

const uri = "your_mongodb_uri";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

async function run() {
    try {
        await client.connect();
        const database = client.db('test_database');
        await database.createCollection('test_collection');
        console.log("Database and collection created");
    } finally {
        await client.close();
    }
}

run().catch(console.dir);

4. CRUD操作

插入文档(Create)

javascript 复制代码
const document = { name: "Alice", age: 25, city: "New York" };
const result = await collection.insertOne(document);
console.log(`${result.insertedId} document inserted`);

读取文档(Read)

javascript 复制代码
const documents = await collection.find({}).toArray();
console.log(documents);

更新文档(Update)

javascript 复制代码
const filter = { name: "Alice" };
const update = { $set: { age: 26 } };
const result = await collection.updateOne(filter, update);
console.log(`${result.modifiedCount} document updated`);

删除文档(Delete)

javascript 复制代码
const filter = { name: "Alice" };
const result = await collection.deleteOne(filter);
console.log(`${result.deletedCount} document deleted`);

5. 索引

索引用于提高查询性能。MongoDB支持多种类型的索引,包括单字段索引、复合索引、唯一索引等。

示例代码:创建索引

javascript 复制代码
await collection.createIndex({ name: 1 }); // 1表示升序,-1表示降序

6. 聚合管道

聚合管道提供了对集合中的文档进行转换和聚合的功能。

示例代码:使用聚合管道

javascript 复制代码
const pipeline = [
    { $match: { age: { $gte: 25 } } },
    { $group: { _id: "$city", total: { $sum: 1 } } },
    { $sort: { total: -1 } }
];
const results = await collection.aggregate(pipeline).toArray();
console.log(results);

7. 复制与分片

复制

MongoDB支持主从复制和副本集,以确保数据的高可用性。

分片

分片将数据分布到多个服务器上,以提高大数据集的性能和可扩展性。


总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,自学记录MongoDB基础知识点总览。

相关推荐
博睿谷IT99_2 分钟前
MySQL OCP 认证限时免费活动 7 月 31 日 前截止!!!
数据库·mysql·开闭原则
scdifsn5 分钟前
动手学深度学习12.1. 编译器和解释器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·编辑器·解释器·命令式编程·符号式编程
博睿谷IT99_6 分钟前
MySQL OCP和Oracle OCP怎么选?
数据库·mysql·oracle·开闭原则·ocp
Kazefuku10 分钟前
Excel学习笔记
笔记·学习·excel
hnlucky38 分钟前
《Zabbix Proxy分布式监控实战:从安装到配置全解析》
数据库·分布式·学习·adb·zabbix·集成学习·proxy模式
Catfood_Eason1 小时前
初识MySQL
数据库·mysql
熊猫的反手凶变直线1 小时前
Java-Lambda 表达式
java·开发语言·windows·笔记
怪小庄吖1 小时前
7系列 之 OSERDESE2
笔记·fpga开发·硬件架构·硬件工程·xilinx·7系列fpga·i/o资源
怪小庄吖1 小时前
UG471 之 SelectIO 逻辑资源
笔记·fpga开发·硬件架构·硬件工程·xilinx·7系列fpga·i/o资源
光电大美美-见合八方中国芯1 小时前
【平面波导外腔激光器专题系列】1064nm单纵模平面波导外腔激光器‌
网络·数据库·人工智能·算法·平面·性能优化