🗂️ Working with MongoDB Indexes
🗂️ 使用 MongoDB 索引
📚 Overview of Indexes
📚 索引概述
- Definition: A database index functions like a book’s index, allowing faster query results by referencing an ordered list instead of scanning the entire dataset.
- 定义:数据库索引的功能类似于书的索引,通过引用有序列表而不是扫描整个数据集来加快查询结果。
- Collection Scan: A query that does not use an index; it examines all documents, leading to slower performance.
- 集合扫描:不使用索引的查询;它会检查所有文档,导致性能下降。
🔍 Types of Indexes
🔍 索引类型
- Single Field Index: Indexing on one field (e.g.,
username). - 单字段索引:在一个字段上创建索引(例如,
username)。 - Compound Index: Indexing on multiple fields (e.g.,
cuisineandrating). - 复合索引:在多个字段上创建索引(例如,
cuisine和rating)。
🚀 Creating an Index
🚀 创建索引
- To create an index, use the command:
- 要创建索引,请使用以下命令:
1 | db.collection.createIndex({ field: 1 }) // 1 for ascending order |
1 | db.collection.createIndex({ field: 1 }) // 1 代表升序 |
Example of creating a compound index:
创建复合索引的示例:
1
db.restaurants.createIndex({ cuisine: 1, rating: -1 })
📈 Evaluating Index Usage
📈 评估索引使用情况
Use the
explaincommand to analyze query performance:使用
explain命令分析查询性能:1
db.users.find({"username": "user101"}).explain("executionStats")
🗺️ Geospatial Indexes
🗺️ 地理空间索引
MongoDB supports two types of geospatial indexes: 2d and 2dsphere, which are used for querying geographical data.
MongoDB 支持两种类型的地理空间索引:2d 和 2dsphere,用于查询地理数据。
Example of creating a geospatial index:
创建地理空间索引的示例:
1
db.restaurants.createIndex({ location: "2dsphere" })
⏳ Time Series Data
⏳ 时间序列数据
- Understanding how to create and manage time series collections is crucial.
- 理解如何创建和管理时间序列集合至关重要。
- Set granularity for time series data to optimize performance.
- 为时间序列数据设置粒度以优化性能。
💡 Key Takeaways
💡 核心要点
- Always create indexes to support query patterns for efficient data retrieval.
- 始终创建索引以支持查询模式,从而实现高效的数据检索。
- Geospatial queries are critical in location-based applications, enhancing user experience with minimal latency.
- 地理空间查询在基于位置的应用程序中至关重要,能以最小的延迟增强用户体验。
- Regularly evaluate the effectiveness of your indexes using the
explaincommand to ensure optimal performance. - 定期使用
explain命令评估索引的有效性,以确保最佳性能。
📚 MongoDB Indexing, Geospatial Data, and Time Series
📚 MongoDB 索引、地理空间数据和时间序列
🗂️ Indexing in MongoDB
🗂️ MongoDB 中的索引
Compound Index:
复合索引:
An index on more than one field, useful for queries with multiple sort directions.
针对多个字段的索引,对于具有多个排序方向的查询非常有用。
- Example:
db.users.createIndex({"age": 1, "username": 1}) - 示例:
db.users.createIndex({"age": 1, "username": 1})
- Example:
Natural Order:
自然顺序:
- Querying without sorting returns documents in their natural order.
- 不进行排序的查询会按自然顺序返回文档。
📊 Geospatial Indexes
📊 地理空间索引
- Types of Geospatial Indexes:
- 地理空间索引的类型:
- 2dsphere: Supports spherical geometries, accounting for Earth’s curvature.
- 2dsphere:支持球面几何,考虑了地球的曲率。
- 2d: Works with flat, two-dimensional plane geometries.
- 2d:适用于平面二维几何。
- GeoJSON Format:
- GeoJSON 格式:
- Point:
{"type": "Point", "coordinates": [longitude, latitude]} - 点 (Point):
{"type": "Point", "coordinates": [经度, 纬度]} - Line:
{"type": "LineString", "coordinates": [[lon1, lat1], [lon2, lat2]]} - 线 (LineString):
{"type": "LineString", "coordinates": [[经度1, 纬度1], [经度2, 纬度2]]} - Polygon:
{"type": "Polygon", "coordinates": [[[lon1, lat1], [lon2, lat2], ...]]} - 多边形 (Polygon):
{"type": "Polygon", "coordinates": [[[经度1, 纬度1], [经度2, 纬度2], ...]]}
- Point:
🌍 Types of Geospatial Queries
🌍 地理空间查询的类型
- Intersection: Find documents that intersect with a specified location.
- 相交 (Intersection):查找与指定位置相交的文档。
- Example: Using
$geoIntersects - 示例:使用
$geoIntersects
- Example: Using
- Within: Query for documents completely contained in a specified area.
- 包含在内 (Within):查询完全包含在指定区域内的文档。
- Example: Using
$geoWithin - 示例:使用
$geoWithin
- Example: Using
- Near: Find nearby locations, automatically sorted by distance.
- 邻近 (Near):查找附近的位置,并按距离自动排序。
- Example: Using
$near - 示例:使用
$near
- Example: Using
📝 Creating Geospatial Indexes
📝 创建地理空间索引
2dsphere Index:
2dsphere 索引:
1
db.collection.createIndex({ <location field>: "2dsphere" });
1
db.collection.createIndex({ <位置字段>: "2dsphere" });
2d Index:
2d 索引:
1
db.collection.createIndex({ <location field>: "2d" });
1
db.collection.createIndex({ <位置字段>: "2d" });
🔍 Text Indexing
🔍 文本索引
Creating a Text Index:
创建文本索引:
Enables text searches on specified fields.
在指定字段上启用文本搜索。
- Example:
- 示例:
1
db.articles.createIndex({"title": "text", "body": "text"});
Text Search Example:
文本搜索示例:
1
db.articles.find({$text: {$search: "\"impact crater\" lunar"}}, {title: 1}).limit(10);
- This query retrieves articles containing “impact crater” and “lunar”.
- 此查询检索包含 “impact crater” 和 “lunar” 的文章。
⏳ Time Series Data
⏳ 时间序列数据
- Components of Time Series Data:
- 时间序列数据的组成部分:
- Timestamp: The time when the data point was recorded.
- 时间戳 (Timestamp):数据点被记录的时间。
- Metadata: Identifies the data series, stored in a
metaField. - 元数据 (Metadata):标识数据序列,存储在
metaField中。 - Measurements: The actual data values or metrics.
- 度量值 (Measurements):实际的数据值或指标。
📝 Key Notes
📝 关键说明
- A compound index improves query efficiency for complex queries.
- 复合索引可以提高复杂查询的效率。
- Geospatial indexes allow for effective querying of spatial data with specific operational commands.
- 地理空间索引允许使用特定的操作命令对空间数据进行有效查询。
- Text indexing is crucial for efficient searching of large text datasets.
- 文本索引对于高效搜索大型文本数据集至关重要。
- Time series data analysis relies on understanding changes over time, essential for trend analysis.
- 时间序列数据分析依赖于对随时间变化的理解,这对于趋势分析至关重要。
📊 Working with MongoDB Indexes, Geospatial Data, and Time Series
📊 使用 MongoDB 索引、地理空间数据和时间序列
🗂️ Creating and Querying Time Series Collections
🗂️ 创建和查询时间序列集合
Time Series Collections: Used for storing time-stamped data points.
时间序列集合:用于存储带时间戳的数据点。
Creation:
创建:
Use
db.createCollection()method or command:使用
db.createCollection()方法或命令:1
2
3
4
5
6db.createCollection("weather", {
timeseries: {
timeField: "timestamp",
metaField: "metadata"
}
})1
2
3
4
5
6db.createCollection("weather", {
timeseries: {
timeField: "timestamp", // 时间字段
metaField: "metadata" // 元数据字段
}
})Specify
timeFieldandmetaField.指定
timeField和metaField。
Defining Time Interval:
定义时间间隔:
Set granularity or bucket properties:
设置粒度或存储桶属性:
1
2
3
4
5
6timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "seconds" // or
bucketMaxSpanSeconds: "300"
}1
2
3
4
5
6timeseries: {
timeField: "timestamp", // 时间字段
metaField: "metadata", // 元数据字段
granularity: "seconds" // 或者 // 粒度:"秒"
bucketMaxSpanSeconds: "300" // 存储桶最大跨度秒数:"300"
}
Listing Time Series Collections:
列出时间序列集合:
Use
listCollectionscommand:使用
listCollections命令:1
2
3
4db.runCommand({
listCollections: 1,
filter: { type: "timeseries" }
})1
2
3
4db.runCommand({
listCollections: 1,
filter: { type: "timeseries" } // 筛选条件:类型为 "timeseries"
})
🔍 MongoDB Index Types
🔍 MongoDB 索引类型
- MongoDB supports several index types:
- MongoDB 支持多种索引类型:
- Single Field Index
- 单字段索引
- Compound Index
- 复合索引
- Geospatial Index
- 地理空间索引
All of the above are supported by MongoDB.
MongoDB 支持以上所有类型。
📈 Summary of Key Concepts
📈 关键概念总结
- Index Types: Improve query performance; includes single field, compound, geospatial, hashed, and text indexes.
- 索引类型:提高查询性能;包括单字段、复合、地理空间、哈希和文本索引。
- Creating Indexes: Use
db.collection.createIndex()for specified fields. - 创建索引:对指定字段使用
db.collection.createIndex()。 - Geospatial Indexing: Necessary for location-based services; supports spherical geometries with
2dsphereand planar geometries with2d. - 地理空间索引:对于基于位置的服务是必需的;通过
2dsphere支持球面几何,通过2d支持平面几何。 - Time Series Data: Efficiently stored and queried; includes timestamps, metadata, and measurements.
- 时间序列数据:高效存储和查询;包括时间戳、元数据和度量值。
- Performance Optimization: Proper index selection crucial for efficient queries; use
$geoIntersects,$geoWithin, and$nearfor geospatial queries. - 性能优化:正确的索引选择对于高效查询至关重要;对地理空间查询使用
$geoIntersects、$geoWithin和$near。 - Aggregation Pipelines: Can be utilized alongside indexes for complex data transformations.
- 聚合管道:可以与索引一起用于复杂的数据转换。
⚙️ Important Note on Indexes
⚙️ 关于索引的重要说明
- Regularly review and update indexes based on application query patterns and performance metrics to maintain optimal performance and scalability.
- 根据应用程序查询模式和性能指标定期审查和更新索引,以保持最佳性能和可伸缩性。