使用 MongoDB 视图转换文档并过滤 Atlas Search 的集合
您可以在 视图上创建 Atlas Search 索引,以转换文档和集合,从而可以部分索引集合、支持不兼容的数据类型或数据模型等。
注意
将 MongoDB 视图与 Atlas Search 结合使用是一项预览功能。预览期间,功能和相应的文档可能随时更改。
以下示例使用了 sample_mflix 和 sample_airbnb 示例数据库。
要求
您必须使用:
MongoDB 8.0 或更高版本。
在预览期间,您必须使用:
限制
Atlas Search 仅支持在以下阶段
$expr
的视图:索引名称在源集合及其所有视图中必须是唯一的。
Atlas Search不支持会生成动态结果的操作符的视图定义,例如$$USER_ROLES系统变量和$rand聚合操作符。
Atlas Search 查询返回源集合中显示的原始文档。
示例:过滤文档
要创建视图,您必须拥有 createCollection
权限。
您可以对集合进行部分索引,以过滤文档。以下示例在 sample_mflix.movies
集合上创建一个视图,以便您可以仅搜索在 2000 年 1 月 1 日之后发行的电影。
使用 mongosh
连接到 Atlas 集群。
要了解更多信息,请参阅通过 mongosh
进行连接。
开始您的索引配置。
在页面上进行以下选择,然后单击 Next。
Search Type | 选择 Atlas Search 索引类型。 |
Index Name and Data Source | 指定以下信息:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
在 releasedAfter2000Index
部分索引上运行查询。
注意
以下示例通过对源集合 movies
运行 .aggregate
命令来查询 releasedAfter2000Index
索引。在预览期间,您必须使用视图的源集合 (movies
) 来查询在视图上创建的搜索索引。如果您查询视图(movies_ReleasedAfter2000
),Atlas Search 不会返回任何结果。
use sample_mflix
1 db.movies.aggregate([ 2 { 3 $search: { 4 index: "releasedAfter2000Index", 5 text: { 6 path: "title", 7 query: "foo" 8 }, 9 sort: { 10 released: 1 11 } 12 } 13 } 14 ])
[ { _id: ObjectId('573a13d2f29313caabd929f8'), plot: "Rising from the ashes of Nirvana, the Foo Fighters became a Grammy-winning sensation on their own. Sixteen years of the band's history comes to life in this documentary, from their demo ...", genres: [ 'Documentary', 'Music' ], runtime: 150, cast: [ 'Shawn Cloninger', 'William Goldsmith', 'Jessy Greene', 'Dave Grohl' ], num_mflix_comments: 0, poster: 'https://m.media-amazon.com/images/M/MV5BMzE4OTczMTgxM15BMl5BanBnXkFtZTcwNTU1NjQxOA@@._V1_SY1000_SX677_AL_.jpg', title: 'Foo Fighters: Back and Forth', fullplot: `Rising from the ashes of Nirvana, the Foo Fighters became a Grammy-winning sensation on their own. Sixteen years of the band's history comes to life in this documentary, from their demo tapes through the creation of their 2011 album, "Wasting Light."`, languages: [ 'English' ], released: ISODate('2011-04-05T00:00:00.000Z'), directors: [ 'James Moll' ], awards: { wins: 1, nominations: 1, text: '1 win & 1 nomination.' }, lastupdated: '2015-08-19 00:00:25.937000000', year: 2011, imdb: { rating: 8.4, votes: 3745, id: 1853563 }, countries: [ 'USA' ], type: 'movie', tomatoes: { viewer: { rating: 4.4, numReviews: 857, meter: 96 }, dvd: ISODate('2011-08-08T00:00:00.000Z'), website: 'http://us.foofightersfilm.com/', production: 'Cinedigm Digital Cinema', lastUpdated: ISODate('2015-09-12T18:42:01.000Z') } } ]
示例:添加或修改字段
以下示例允许您根据新的 totalPrice
字段(该字段是 price
和 cleaningFee
字段的总和)在 sample_airbnb.listingsAndReviews
集合中搜索住宿信息。此外,由于 Atlas Search 不支持 Decimal128
类型,我们将值转换为 Double
。
使用 mongosh
连接到 Atlas 集群。
要了解更多信息,请参阅通过 mongosh
进行连接。
开始您的索引配置。
在页面上进行以下选择,然后单击 Next。
Search Type | 选择 Atlas Search 索引类型。 |
Index Name and Data Source | 指定以下信息:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
在 totalPriceIndex
索引上运行查询。
注意
以下示例通过对源集合 listingsAndReviews
运行 .aggregate
命令来查询 totalPriceIndex
索引。在预览期间,您必须使用视图的源集合 (listingsAndReviews
) 来查询在视图上创建的搜索索引。如果您查询视图(listingsAndReviews_totalPrice
),Atlas Search 不会返回任何结果。
use sample_airbnb
1 db.listingsAndReviews.aggregate([ 2 { 3 $search: { 4 index: "totalPriceIndex", 5 range: { 6 path: "totalPrice", 7 lte: 300 8 }, 9 returnStoredSource: true 10 } 11 } 12 ])
[ { _id: '10006546', totalPrice: 115 }, { _id: '1001265', totalPrice: 215 }, { _id: '10021707', totalPrice: 40 }, { _id: '1003530', totalPrice: 270 }, { _id: '10038496', totalPrice: 269 }, { _id: '10051164', totalPrice: 250 }, { _id: '10057447', totalPrice: 50 }, { _id: '10057826', totalPrice: 205 }, { _id: '10059244', totalPrice: 43 }, { _id: '10066928', totalPrice: 140 }, { _id: '10082422', totalPrice: 60 }, { _id: '10083468', totalPrice: 40 }, { _id: '10084023', totalPrice: 231 }, { _id: '10091713', totalPrice: 231 }, { _id: '10092679', totalPrice: 58 }, { _id: '10096773', totalPrice: 205 }, { _id: '10112159', totalPrice: 90 }, { _id: '10117617', totalPrice: 55 }, { _id: '10120414', totalPrice: 150 }, { _id: '10133554', totalPrice: 121 } ]
编辑视图
以下示例更新了 2000 年之前电影的 movies_ReleasedAfter2000
MongoDB 视图。
db.runCommand( { collMod: "movies_ReleasedAfter2000", viewOn: "movies", "pipeline": [ { $match: { $expr: { $lt: [ "$released", ISODate("2000-01-01T00") ] } } } ] } )
运行此命令后,Atlas Search 会自动检测到 View 定义的更改,并在不中断服务的情况下执行重新索引。
返回视图的管道
以下示例返回 movies_ReleasedAfter2000
视图中的管道。
1 db.getCollectionInfos({ name: "movies_ReleasedAfter2000" })[0].options.pipeline
[ { '$match': { '$expr': { '$gt': [ '$released', ISODate('2000-01-01T00:00:00.000Z') ] } } } ]
故障排除
在以下场景中,索引会更改为 FAILED
状态:
您在与 Atlas Search 不兼容的视图上创建索引。
您编辑视图的方式不符合 Atlas Search 兼容性要求。
您删除或更改视图的源集合。
例如,如果一个视图是在另一个视图上创建的,并且将父视图源更改为另一个集合。
注意
如果一个视图是其他视图的后代,这一限制也同样适用。例如,您无法更改或移除所有后代所源自的源集合。
索引在以下情况下会停滞:
警告
如果在视图中定义的聚合管道与集合中的文档不兼容,搜索复制将失败。例如,如果 $toDouble
表达式对包含数组的文档字段进行操作,复制将失败。确保您的视图在处理集合中的所有文档时不会出错。
如果视图定义在索引为
READY
时导致聚合失败,则索引将变为STALE
。在您解析文档或更改视图定义后,索引将返回到READY
,以确保不再失败。不过,在复制自动从 oplog. 删除之前,索引是可以查询的。如果视图定义在索引为
BUILDING
时导致聚合管道失败,则在修复文档之前,则索引生成将停滞。在您解析文档或更改视图定义后,索引将返回到READY
,以确保不再失败。
您可以在 Atlas 用户界面的索引状态详情页面查看索引状态。
索引流程
当您在视图上创建 Atlas Search 索引时,mongot
进程执行的任务与在常规集合上创建 Atlas Search 索引时相同。mongot
进程:
根据该集合的索引定义中的规则来创建 Atlas Search 索引。
监控文档当前状态的变更流,以及已定义 Atlas Search 索引的集合索引。
处理 Atlas Search 查询,并将匹配文档的文档 ID 和其他搜索元数据返回给
mongod
,然后执行完整的文档查找并将结果返回给客户端。
当您在视图上创建 Atlas Search 索引时,视图定义将在步骤 1 和 2 期间应用,转换后的文档将存储在磁盘上的 Atlas Search 索引中。
了解详情
要了解有关视图的更多信息,请参阅视图。
要在视图上创建 Atlas Vector Search 索引,请参阅使用 MongoDB 视图为 Atlas Vector Search 转换文档和过滤集合。