MongoDB - 索引 (二)使用

本文详细介绍了MongoDB中索引相关操作与优化方法,包括创建、删除、重建索引等,以及如何通过命令行查询索引状态和大小,旨在帮助开发者提升数据查询效率。
索引相关操作


Name Description
db.collection.createIndex() Builds an index on a collection. Use db.collection.ensureIndex().
db.collection.dropIndex() Removes a specified index on a collection.
db.collection.dropIndexes() Removes all indexes on a collection.
db.collection.ensureIndex() Creates an index if it does not currently exist. If the index existsensureIndex() does nothing.
db.collection.getIndexes() Returns an array of documents that describe the existing indexes on a collection.
db.collection.getIndexStats() Renders a human-readable view of the data collected by indexStats which reflects B-tree utilization.
db.collection.indexStats() Renders a human-readable view of the data collected by indexStats which reflects B-tree utilization.
db.collection.reIndex() Rebuilds all existing indexes on a collection.
db.collection.totalIndexSize() Reports the total size used by the indexes on a collection. Provides a wrapper around the totalIndexSize field of the collStats output.
cursor.explain() Reports on the query execution plan, including index use, for a cursor.
cursor.hint() Forces MongoDB to use a specific index for a query.
cursor.max() Specifies an exclusive upper index bound for a cursor. For use withcursor.hint()
cursor.min() Specifies an inclusive lower index bound for a cursor. For use withcursor.hint()
cursor.snapshot() Forces the cursor to use the index on the _id field. Ensures that the cursor returns each document, with regards to the value of the _id field, only once.
Name Description
dropIndexes Removes indexes from a collection.
compact Defragments a collection and rebuilds the indexes.
reIndex Rebuilds all indexes on a collection.
validate Internal command that scans for a collection’s data and indexes for correctness.
indexStats Experimental command that collects and aggregates statistics on all indexes.
geoNear Performs a geospatial query that returns the documents closest to a given point.
geoSearch Performs a geospatial query that uses MongoDB’s haystack index functionality.
geoWalk An internal command to support geospatial queries.
checkShardingIndex Internal command that validates index on shard key.

Geospatial Query Selectors

Name Description
$geoWithin Selects geometries within a bounding GeoJSON geometry.
$geoIntersects Selects geometries that intersect with a GeoJSON geometry.
$near Returns geospatial objects in proximity to a point.
$nearSphere Returns geospatial objects in proximity to a point on a sphere.

Indexing Query Modifiers

Name Description
$explain Forces MongoDB to report on query execution plans. See explain().
$hint Forces MongoDB to use a specific index. See hint()
$max Specifies an exclusive upper limit for the index to use in a query. Seemax().
$min Specifies an inclusive lower limit for the index to use in a query. See min().
$returnKey Forces the cursor to only return fields included in the index.
$snapshot Forces the query to use the index on the _id field. See snapshot()


后台建立索引 db.people.ensureIndex( { zipcode: 1}, {background: true} ) 

索引相关命令:http://docs.mongodb.org/manual/reference/indexes/


<think>我们正在尝试安装MongoDB的特定版本5.0.2。在Ubuntu系统中,MongoDB提供了自己的仓库,我们可以通过添加该仓库并指定版本进行安装。 步骤如下: 1. 导入MongoDB的GPG密钥 2.MongoDB创建一个list文件(在/etc/apt/sources.list.d/目录下) 3. 更新本地包数据库 4. 安装指定版本的MongoDB包 但是,注意:MongoDB的版本号在包名中通常只包含主版本和次版本(如5.0),而不会包含修订版本(如5.0.2)。实际上,安装时我们指定主次版本,然后通过更新来获取最新的修订版本。但我们可以指定安装5.0.2版本,如果该版本在仓库中存在的话。 然而,通常情况下,我们安装的是5.0版本,然后系统会自动安装该系列的最新修订版(包括5.0.2)。但如果我们必须确保安装的是5.0.2,那么我们需要明确指定每个组件的版本号。 根据MongoDB的安装文档,我们可以这样安装指定版本: 首先,添加MongoDB的仓库。这里我们使用Ubuntu 20.04(focal)作为例子,如果你使用的是其他版本,请替换`focal`为相应的代号。 步骤: 1. 导入公钥 wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add - 2. 创建list文件 echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list 3. 更新包列表 sudo apt update 4. 安装指定版本:我们可以使用以下命令安装特定版本(例如5.0.2): sudo apt install mongodb-org=5.0.2 mongodb-org-database=5.0.2 mongodb-org-server=5.0.2 mongodb-org-shell=5.0.2 mongodb-org-mongos=5.0.2 mongodb-org-tools=5.0.2 注意:每个组件都需要单独指定版本号,并且版本号必须完全匹配。 但是,请注意:在安装时,可能会遇到依赖问题,因为每个组件的依赖可能要求相同的版本。因此,我们通常将所有的组件都指定为相同的版本。 另外,为了防止自动更新(因为MongoDB建议手动升级),我们可以将MongoDB的包标记为保留,这样就不会在系统更新时自动升级了: sudo apt-mark hold mongodb-org mongodb-org-database mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools 因此,完整的命令序列如下: 注意:请确保你使用的Ubuntu版本与MongoDB仓库支持的版本一致。这里以Ubuntu 20.04 (focal)为例。 如果系统不是focal,请替换为正确的代号(如Ubuntu 18.04是bionic,Ubuntu 22.04是jammy)。但请注意,MongoDB 5.0支持Ubuntu 20.04和18.04,不支持22.04(因为5.0发布时22.04还未发布)。如果使用22.04,可能需要使用其他方法。 下面给出完整的命令:</think>要在 Ubuntu 上安装 MongoDB 5.0.2 的特定版本,需要执行以下步骤: ### 完整安装步骤: ```bash # 1. 导入 MongoDB 公钥 wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add - # 2. 创建 MongoDB 5.0 的源列表文件 echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list # 3. 更新软件包索引 sudo apt update # 4. 安装指定版本 (5.0.2) sudo apt install -y \ mongodb-org=5.0.2 \ mongodb-org-database=5.0.2 \ mongodb-org-server=5.0.2 \ mongodb-org-shell=5.0.2 \ mongodb-org-mongos=5.0.2 \ mongodb-org-tools=5.0.2 # 5. 防止自动更新 sudo apt-mark hold mongodb-org* ``` ### 关键点解释: 1. **版本锁定**: - 使用 `=` 指定确切版本号(`5.0.2`) - 必须为所有组件指定相同版本 2. **apt-mark hold**: - 防止系统自动更新 MongoDB - 保持版本固定在 5.0.2 3. **版本兼容性**: - MongoDB 5.0 支持 Ubuntu 18.04/20.04/22.04 - `$(lsb_release -cs)` 自动获取当前系统代号 ### 验证安装: ```bash # 查看安装版本 mongod --version # 应显示:db version v5.0.2 # 启动服务 sudo systemctl start mongod # 检查运行状态 sudo systemctl status mongod ``` ### 常见问题处理: 1. **依赖错误**: ```bash # 先尝试修复依赖 sudo apt --fix-broken install # 清除无效包 sudo apt autoclean ``` 2. **版本不可用**: - 确保源地址正确:`https://repo.mongodb.org/apt/ubuntu` - 检查[官方安装文档](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/) 3. **升级现有版本**: ```bash # 先卸载旧版 sudo apt purge mongodb-org* sudo rm -r /var/log/mongodb /var/lib/mongodb ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值