【MongoDB】深入了解MongoDB不可不知的十点
一、对象ID的生成 每个mongoDB文档那个都要求有一个主键,它在每个集合中对所有的文档必须是唯一的,主键存放在文档_id字段中。由12个字符组成; 4c291856 238d3b 19b2 000001 4字节时间戳 机器ID 进程ID 计数器3333 二、BSON BSON是mongodb中用来标示文档的
一、对象ID的生成
每个mongoDB文档那个都要求有一个主键,它在每个集合中对所有的文档必须是唯一的,主键存放在文档_id字段中。由12个字符组成;
4c291856 238d3b 19b2 000001
4字节时间戳 机器ID 进程ID 计数器3333
二、BSON
BSON是mongodb中用来标示文档的二进制格式,它既是存储格式,也是命令格式。所有文档都以bson存储在磁盘上,所有的查询和命令都用bson文档来指定。
Db.users.find({_id:ObjectId(‘4c291856238d3b19b2000001’)})
Db.users.find({_id:‘4c291856238d3b19b2000001’})
以上两种查询的结果完全不同,其中只有一个能查询到匹配_id字段,这完全取决于users集合中的文档存储的是BSON对象ID还是标示ID十六进制的BSON字符串。
三、聚合命令限制
在实用性方面,distinct 和group有一个很大的限制,它们返回的结果集不能超过16M。16M的限制并不是这些命令本身所强加的阀值,这是所有的初始查询结果的大小。如果distinct和group处理不了你的集合结果集,那么就只能使用map-reduce代替了,它的结果可以保存在集合中的非内联返回。
四、原子文档处理
我们知道mongodb不善于处理事物,但要是用户确实需要需要进行查询和更新同时操作怎么办呢? 有一个工具你肯定不想错过,那就是mongodb的findAndModify命令。该命令允许对文档进行原子性更新,并在同一次调用中返回。
db.collections.findAndModify(
{
query:{},update:{},new:true or false
}
)
默认情况下,findandmodify 命令会返回更新前的文档,要是返回修改后的文档,就把new设置为false.
五、对数组使用$unset
请注意在单个数组元素上使用$unset的结果可能与你设想的不一样。其结果只是将元素的值设置为null,而非删除整个元素。要想彻底删除某个数组元素,可以用$pull 和$pop操作符。
六、$addToSet和$push的区别
该两者的功能都是给数组添加一个值。但是两者之间有区别,$addToSet要添加的值如果不存在才进行添加操作,但是push只添加一个值;例如:
tags = [“tools”,”garden”]
如果执行db.collection.update({},{$push:{tag:tools}}) 结果就是 [“tools”,”garden”,“tools”]
如果执行db.collection.update({},{$addToSet:{tag:tools}}) 结果不变
七、稀疏索引创建
在稀疏索引中只会出现被索引键有值的文档,如果想创建稀疏索引,指定{sparse:true}就可以了。例如:
Db.product.ensureIndex({sku:1},{unique:true,sparse:true})
Sku可能存在为null的文档。
八、声明索引时要小心
由于创建索引比较简单,所以很容易在无意间创建索引,如果数据集很大的话,构建会花费很长的时间。并且没办法中种植。同时创建索引时候最好先排序这样更加高效。
九、用Explain(true)详细查询执行计划
用户db.collection.find(condition).explain(true)
十、乐观锁
乐观锁就是并发控制,这项技术保证在无需锁定记录的情况下对器进行彻底更新。要理解它,最简单的办法就是想像一个wifi,有多个用户可以同时编辑一个页面。但你肯定不希望用户编辑并更新一个过期的页面,这是就可以使用乐观锁协议,当用户试图保存他们更改的时候,会在更新操作中增加一个时间戳,如果该值比这个页面最近保存的版本旧,就不让用户更新。
这个在mongodb 执行$inc中用到

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

Beihang's research team used a diffusion model to "replicate" the Earth? At any location around the world, the model can generate remote sensing images of multiple resolutions, creating rich and diverse "parallel scenes." Moreover, complex geographical features such as terrain, climate, and vegetation have all been taken into consideration. Inspired by Google Earth, Beihang's research team "loaded" satellite remote sensing images of the entire Earth into a deep neural network from an overhead perspective. Based on such a network, the team built MetaEarth, a global top-down visual generation model. MetaEarth has 600 million parameters and can generate remote sensing images with multiple resolutions, unbounded and covering any geographical location around the world. Compared with previous research, the global remote sensing image generation model has

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

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:

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

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.

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies
