MongoDB数据库插入、更新和删除操作详解
一、Insert操作 Insert操作是MongoDB插入数据的基本方法,对目标集合使用Insert操作,会将该文档添加到MongoDB并自动生成相应的ID键。文档结构采用类JSON的BSON式。常见的插入操作主要有单条插入和批量插入两种形式。插入时只是简单地将文档存入数据库中,不
一、Insert操作
Insert操作是MongoDB插入数据的基本方法,对目标集合使用Insert操作,会将该文档添加到MongoDB并自动生成相应的ID键。文档结构采用类似JSON的BSON格式。常见的插入操作主要有单条插入和批量插入两种形式。插入时只是简单地将文档存入数据库中,不进行额外的验证,也不会执行代码,所以不存在注入式攻击的可能。
1、单条插入
2、批量插入
MongoDB对批量插入的支持是通过传递多个文档组成的数组到数据库来实现的。由于它插入数据是通过发送TCP请求的,这样只需发送单个TCP请求,且数据库无需处理大量的消息头,减少插入时间。这种方式的批量插入一次只能将多个文档插入到一个集合中,对于插入到多个集合可以循环调用Insert操作。
二、Remove操作
remove函数可以用来删除数据,它能接受一个文档作为可选参数,只有符合条件的文档才会被删除。删除数据是永久性的,不能撤销,也不能恢复,需要谨慎。删除文档需要清空整个集合,不如直接删除集合快。
三、Update操作
update函数用于修改数据库中的数据,它接收两个参数,一个是查询文档,用来查找要更新的文档,另一个是修改器文档,用来描述对找到的文档做哪些修改。更新操作是原子性的,如果多个更新同时发生,则所有的更新都会执行,但最后的更新是最终赢家。
1、整体更新(文档替换)
2、局部更新(修改器)
部分更新是通过原子的更新修改器实现的,使用修改器时除了”_id“的值不能改变,其他任何值都能改变。文档替换是可以改变所有值的。
$inc修改器:将指定属性的值增加特定的步长,如果键不存在则创建它。
$set修改器:用来指定一个键的值,如果不存在则创建它。
$push:数组修改器,如果指定的键存在,则向已有的数组末尾加入一个元素,键不存在则会创建一个新的数组。
3、upsert操作
upsert操作具有saveOrUpdate的功能,如果没有文档符合更新条件,则以更新条件和更新文档为基础创建一个新的文档。如果有符合更新条件的文档,则正常更新。创建新文档时会以条件文档作为基础,将修改器作用其之上。upsert操作是原子性的,高效的。
4、批量更新
批量更新只需将update的第4个参数设为true即可。
四、瞬间完成
上面的插入,删除和更新操作都是瞬间完成的,它们不需要等待数据库响应。这样的实现可以获取高性能,速度非常快,只会受客户端发送速度和网络速度的制约。但由于不会获取服务器状态,所以不能保证操作顺利完成。这对于付费系统,安全性较高的系统是不可行的,此时对这些操作需要使用它们的安全版本。安全版本会在操作执行后立即运行getLastError命令,来检查是否执行成功。如果失败一般会抛出可捕获的异常,然后我们可以在代码中处理。
五、请求和连接
数据库会为每个MongoDB数据库连接创建一个队列,存放这个连接的请求,客户端新发送的请求会被放到队列的末尾。只有队列中的请求都执行完毕,后续的请求才会执行。即对于单个连接来说,请求都是顺序执行不存在并发问题,所以它总能读到自己写的东西。但对于不同的连接就有可能出现读取和写入不一致的问题,在驱动程序使用连接池时要特别注意此行为。具体连接池信息可参考官网

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512
