您可以在 视图 上创建 MongoDB Search 索引,以转换文档和集合,从而能够部分索引集合、支持不兼容的数据类型或数据模型等。
The following examples use the sample_mflix and sample_airbnb sample databases.
要求
您必须使用 MongoDB 8.0 或更高版本。
在 MongoDB v8.0+ 上:
使用Atlas用户界面或Atlas管理API在视图上创建MongoDB搜索索引。
对源集合运行MongoDB Search 查询。参考在视图上创建的MongoDB Search索引。 这些查询返回源集合中出现的原始文档。
在 MongoDB v8.1+ 上,您还可以:
使用
mongosh和 驱动程序 方法、db.collection.createSearchIndex()、db.collection.updateSearchIndex()、db.collection.dropSearchIndex()和$listSearchIndexes在视图上创建和管理MongoDB搜索索引。针对该视图运行MongoDB搜索查询。
限制
MongoDB Search 支持具有以下阶段的视图:
索引名称在源集合及其所有视图中必须是唯一的。
MongoDB Search doesn't support view definitions with operators that produce dynamic results, such as the $$USER_ROLES system variable and the
$randaggregation operator.MongoDB Search 查询会返回源集合中出现的原始文档。
To retrieve the transformed document, use the
storedSourceoption.
所需权限
要创建视图,您的角色必须拥有 createCollection 权限。
示例
以下示例展示了如何创建视图、部分索引文档以及对索引运行查询。这些示例通过对源集合或视图运行 .aggregate 命令来查询索引。MongoDB Search 仅在 MongoDB v8.1 或更高版本中支持直接对 View 进行查询。
您可以使用过滤器筛选文档以对集合进行部分索引。以下示例在 sample_mflix.movies 集合上创建一个视图,以便您可以仅搜索在 2000 年 1 月 1 日之后发行的电影。
使用mongosh 连接到您的集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
在Atlas中,转到项目的 Clusters 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
在Atlas中,转到集群的 Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer go 到 MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,请单击 Create cluster 创建一个。要学习;了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Search。
开始您的索引配置。
在页面上进行以下选择,然后单击 Next。
Search Type | 选择 MongoDB Search 索引类型。 |
Index Name and Data Source | 指定以下信息:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
重要提示:您的 |fts|索引默认名为“autoembed_index”。如果您要创建多个索引,我们建议您在所有索引之间保持一致的描述性命名约定。
查询 releasedAfter2000Index 部分索引。
[ { "$search": { "index": "releasedAfter2000Index", "text": { "path": "title", "query": "foo" }, "sort": { "released": 1 } } } ]
SCORE: 3.120296001434326 _id: “573a13d2f29313caabd929f8” awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Rising from the ashes of Nirvana, the Foo Fighters became a Grammy-win…" genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-08-19 00:00:25.937000000" num_mflix_comments: 0 plot: "Rising from the ashes of Nirvana, the Foo Fighters became a Grammy-win…" poster: "https://m.media-amazon.com/images/M/MV5BMzE4OTczMTgxM15BMl5BanBnXkFtZT…" released: 2011-04-05T00:00:00.000+00:00 runtime: 150 title: "Foo Fighters: Back and Forth" tomatoes: Object type: "movie" year: 2011
使用 mongosh 连接到MongoDB部署。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
查询 releasedAfter2000Index 部分索引。
注意
以下示例通过对名为 movies_ReleasedAfter2000 的视图运行 .aggregate 命令来查询 releasedAfter2000Index 索引。如果您的集群正在运行 MongoDB v8.0,则必须使用视图上的索引查询源集合(例如,movies)。升级到 MongoDB v8.1+ 以直接查询视图。
db.movies_ReleasedAfter2000.aggregate([ { $search: { index: "releasedAfter2000Index", text: { path: "title", query: "foo" }, sort: { released: 1 } } } ])
[ { _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字段在 sample_airbnb.listingsAndReviews集合中搜索住宿,该字段是 price 和 cleaningFee 字段的总和。此外,由于MongoDB Search 不支持Decimal128 类型,因此我们将值转换为 Double。
使用mongosh 连接到集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
在Atlas中,转到项目的 Clusters 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
在Atlas中,转到集群的 Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer go 到 MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,请单击 Create cluster 创建一个。要学习;了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Search。
对 totalPriceIndex索引运行查询。
[ { "$search": { "index": "totalPriceIndex", "range": { "path": "totalPrice", "lte": 300 }, "returnStoredSource": true } } ]
SCORE: 1 _id: "10006546" totalPrice: 115 SCORE: 1 _id: "1001265" totalPrice: 215 SCORE: 1 _id: "10021707" totalPrice: 40 SCORE: 1 _id: "1003530" totalPrice: 270 SCORE: 1 _id: "10038496" totalPrice: 269 SCORE: 1 _id: "10051164" totalPrice: 250 SCORE: 1 _id: "10057447" totalPrice: 50 SCORE: 1 _id: "10057826" totalPrice: 205 SCORE: 1 _id: "10059244" totalPrice: 43 SCORE: 1 _id: "10066928" totalPrice: 140
使用 mongosh 连接到MongoDB 部署。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
对 totalPriceIndex索引运行查询。
注意
以下示例通过对名为 listingsAndReviews_totalPrice 的视图运行 .aggregate 命令来查询 totalPriceIndex 索引。如果您的集群正在运行 MongoDB v8.0,则必须使用视图上的索引查询源集合(例如,listingsAndReviews)。升级到 MongoDB v8.1+ 以直接查询视图。
db.listingsAndReviews_totalPrice.aggregate([ { $search: { index: "totalPriceIndex", range: { path: "totalPrice", lte: 300 }, returnStoredSource: true } } ])
[ { _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 } ]
要索引与命名模式匹配的字段,请使用 视图 转换数据,使要索引的字段嵌套在子文档中。这样,您就可以使用子文档路径的动态映射,从而可以自动搜索带后缀 _type 的所有新字段,而无需更改索引定义。
以下 View 名为 listings_SearchableTypes 的仅匹配 sample_airbnb.listingsAndReviews 集合中以 _type 结尾的字段名称。具体来说,该 $set阶段添加了一个名为searchable_types 的新字段,其中包含字段名中含有_type 的过滤后字段。该 $arrayToObject包含过滤器输入(整个文档)和条件(与_type 匹配的 regex)。
使用mongosh 连接到您的集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
创建名为 listings_SearchableTypes 的视图。
1 db.createView( 2 "listings_SearchableTypes", 3 "listingsAndReviews", 4 [ 5 { 6 "$set": { 7 "searchable_types": { 8 "$arrayToObject": { 9 "$filter": { 10 "input": { "$objectToArray": "$$ROOT" }, 11 "cond": { 12 "$regexMatch": { 13 "input": "$$this.k", 14 "regex": /_type$/ 15 } 16 } 17 } 18 } 19 } 20 } 21 } 22 ] 23 )
在Atlas中,转到项目的 Clusters 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
在Atlas中,转到集群的 Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer go 到 MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,请单击 Create cluster 创建一个。要学习;了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Search。
编辑索引定义。
复制并粘贴以下索引定义来替换用户界面中的默认索引定义:
{ "analyzer": "lucene.standard", "searchAnalyzer": "lucene.standard", "mappings": { "dynamic": false, "fields": { "searchable_types": [ { "dynamic": { "typeSet": "tokenTypeSet" }, "type": "document" } ] } }, "typeSets": [ { "name": "tokenTypeSet", "types": [ { "type": "token" } ] } ] }
查询视图。
以下查询在名为 listings_SearchableTypes 的视图中搜索 house,并带有 private room。要运行此查询,请复制并粘贴查询,然后单击 Search:
[ { "$search": { "index": "listingsSearchableTypes", "compound": { "should": [ { "text": { "path": "searchable_types.property_type", "query": "House" } }, { "text": { "path": "searchable_types.room_type", "query": "Private room" } } ] } } } ]
SCORE: 2.0530142784118652 _id: "10051164" name: "Catete's Colonial Big Hause Room B", searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "1016739" name: 'Private Room (2) in Guest House at Coogee Beach', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "10324377" name: 'Suíte em local tranquilo e seguro', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "1073607" name: 'Bright Inner West Terrace Value+', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "1077686" name: 'BALAT', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "10990260" name: 'The Executive Posh Room', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "10992286" name: 'Holoholo Inn: Rain Forest (Priv-2)', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "11396243" name: 'Beautiful Ensuite Room -Easy access to city', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "1145567" name: 'ROOM + ENSUITE INNER CITY MOD HOUSE', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' SCORE: 2.0530142784118652 _id: "11464859" name: 'The Sassy Lilac Megan Love Room', searchable_types: Object property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed'
使用 mongosh 连接到MongoDB部署。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
在视图上创建MongoDB Search索引。
以下名为 listingsSearchableTypes 的 View 上的索引定义配置了一个 typeSet,以自动将 searchable_types 文档中的所有字段索引为 string 类型。如果您添加与此模式匹配的新字段,MongoDB Search 也将自动为这些字段创建索引。
db.listings_SearchableTypes.createSearchIndex( "listingsSearchableTypes", { "analyzer": "lucene.standard", "searchAnalyzer": "lucene.standard", "mappings": { "dynamic": false, "fields": { "searchable_types": [ { "type": "document", "dynamic": { "typeSet": "tokenTypeSet" } } ] } }, "typeSets": [ { "name": "tokenTypeSet", "types": [ { "type": "token" } ] } ] } )
对 totalPriceIndex索引运行查询。
以下查询在名为 listings_SearchableTypes 的视图中,使用 private room 作为查询词来搜索 house。
db.listings_SearchableTypes.aggregate([ { "$search": { "index": "listingsSearchableTypes", "compound": { "should": [ { "equals": { "path": "searchable_types.property_type", "value": "House" } }, { "equals": { "path": "searchable_types.room_type", "value": "Private room" } } ] } } }, { "$limit": 10 }, { "$project": { "_id": 0, "searchable_types": 1, "name": 1 } } ])
[ { name: "Catete's Colonial Big Hause Room B", searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'Private Room (2) in Guest House at Coogee Beach', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'Suíte em local tranquilo e seguro', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'Bright Inner West Terrace Value+', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'BALAT', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'The Executive Posh Room', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'Holoholo Inn: Rain Forest (Priv-2)', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'Beautiful Ensuite Room -Easy access to city', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'ROOM + ENSUITE INNER CITY MOD HOUSE', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } }, { name: 'The Sassy Lilac Megan Love Room', searchable_types: { property_type: 'House', room_type: 'Private room', bed_type: 'Real Bed' } } ]
要对不支持的字段类型(如 布尔值 、 objectId 或 UUID )进行 分面 ,我们建议将值转换为 string 类型。
以下示例在 sample_airbnb.listingsAndReviews 命名空间上创建一个视图,以便您可以对 boolean 类型 host.host_is_superhost 字段和 objectID 类型 _id 字段进行分面操作。
使用mongosh 连接到您的集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
在Atlas中,转到项目的 Clusters 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
在Atlas中,转到集群的 Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer go 到 MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,请单击 Create cluster 创建一个。要学习;了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Search。
Query the listingsAndReviews Source Collection.
下面的查询在listings_SearchableTypes索引中搜索summary字段的ocean view。查询会检索属于每个不同的_id和host.host_is_superhost的属性数量。要运行此查询,请复制并粘贴查询,然后单击 Search:
[{ "$searchMeta": { "index": "listingsSearchableTypes", "facet":{ "operator": { "text": { "path": "summary", "query": "ocean view" } }, "facets": { "idFacet" : { "type" : "string", "path" : "idString", "numBuckets" : 10, }, "hostFacet": { "type" : "string", "path" : "superHostString" } } } } }]
count: Object lowerBound: 510 facet: Object idFacet: Object buckets: Array (10) 0: Object _id: "10082422" count: 1 1: Object _id: "10166986" count: 1 2: Object _id: "10266175" count: 1 3: Object _id: "10267144" count: 1 4: Object _id: "10317142" count: 1 5: Object _id: "10359729" count: 1 6: Object _id: "10392282" count: 1 7: Object _id: "10548991" count: 1 8: Object _id: "1067052" count: 1 9: Object _id: "1069531" count: 1 hostFacet: Object buckets: Array (2) 0: Object _id: "false" count: 370 1: Object _id: "true" count: 140
使用 mongosh 连接到MongoDB部署。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
创建一个 listings_SearchableTypes 视图。
该视图包含以下两个字段:
idString它包含将_id字段转换为string类型后的值。superHostString它包含将host.host_is_superhost字段转换为string类型后的值。
db.createView( "listings_SearchableTypes", "listingsAndReviews", [ { "$addFields": { "idString": { "$toString": "$_id" }, "superHostString": { "$toString": "$host.host_is_superhost" } } } ] )
查询 listingsSearchableTypes 部分索引。
下面的查询会在listings_SearchableTypes索引中搜索ocean view,位于summary字段。查询会检索属于每个不同_id和host.host_is_superhost的属性数量。
db.listings_SearchableTypes.aggregate([{ "$searchMeta": { "index": "listingsSearchableTypes", "facet":{ "operator": { "text": { "path": "summary", "query": "ocean view" } }, "facets": { "idFacet" : { "type" : "string", "path" : "idString", "numBuckets" : 10, }, "hostFacet": { "type" : "string", "path" : "superHostString" } } } } }])
[ { count: { lowerBound: Long('510') }, facet: { idFacet: { buckets: [ { _id: '10082422', count: Long('1') }, { _id: '10166986', count: Long('1') }, { _id: '10266175', count: Long('1') }, { _id: '10267144', count: Long('1') }, { _id: '10317142', count: Long('1') }, { _id: '10359729', count: Long('1') }, { _id: '10392282', count: Long('1') }, { _id: '10548991', count: Long('1') }, { _id: '1067052', count: Long('1') }, { _id: '1069531', count: Long('1') } ] }, hostFacet: { buckets: [ { _id: 'false', count: Long('370') }, { _id: 'true', count: Long('140') } ] } } } ]
要搜索十进制 128 类型的字段,我们建议将值转换为double类型。
以下示例在sample_airbnb.listingsAndReviews命名空间上创建一个视图,添加一个名为totalPrice的字段,该字段包含将price字段和cleaning_fee字段的值转换为double后的总和。
使用mongosh 连接到您的集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
创建名为 listings_SearchablePrice 的视图。
以下视图包含一个名为 totalPrice 的字段,其中包含 price 和 cleaning-fee 字段值的总和。在添加这些字段的值之前,以下代码将price和cleaning-fee字段的值转换为double类型。
1 db.createView( 2 "listings_SearchablePrice", 3 "listingsAndReviews", 4 [ 5 { 6 "$addFields": { 7 "totalPrice": { 8 "$add": [ 9 { 10 "$ifNull": [{ "$toDouble": "$price" }, 0] 11 }, 12 { 13 "$ifNull": [{ "$toDouble": "$cleaning_fee" }, 0] 14 } 15 ] 16 } 17 } 18 } 19 ] 20 )
在Atlas中,转到项目的 Clusters 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
在Atlas中,转到集群的 Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer go 到 MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,请单击 Create cluster 创建一个。要学习;了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Search。
查询源集合。
以下查询在名为 listingsSearchablePrice 的索引中搜索成本在 100 到 200(含)之间的属性。要运行此查询,请复制并粘贴查询,然后单击 Search:
[ { "$search": { "index": "listingsSearchablePrice", "range": { "path": "totalPrice", "gte": 100, "lte": 200 } } } ]
SCORE: 1 _id: "10006546" access: "We are always available to help guests. The house is fully available t…" accommodates: 8 address: Object price: 80.00 SCORE: 1 _id: "10066928" access: "Le logement sera disponible en entier pour votre séjour." accommodates: 6 address: Object price: 140.00 SCORE: 1 _id: "10120414" access: "You can access to the rooftop where you have a beautiful view of Empir…" accommodates: 3 address: Object price: 150.00 SCORE: 1 _id: "10133554" access: "We have bathroom,wc,İnternet in rooms and under rooms we have cafe bar…" accommodates: 3 address: Object price: 121.00 SCORE: 1 _id: "10186755" access: "" accommodates: 2 address: Object price: 185.00 SCORE: 1 _id: "10213499" access: "Guests have access to backyard saltwater pool" accommodates: 2 address: Object price: 117.00 SCORE: 1 _id: "1022200" access: "" accommodates: 6 address: Object price: 135.00 SCORE: 1 _id: "10228731" access: "O quarto é privativo, com acesso a todas as acomodações do apartamento…" accommodates: 1 address: Object price: 149.00 SCORE: 1 _id: "10359729" access: "" accommodates: 4 address: Object price: 105.00 SCORE: 1 _id: "1036027" access: "" accommodates: 8 address: Object price: 100.00
使用 mongosh 连接到MongoDB部署。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
创建一个 listings_SearchablePrice 视图。
以下视图包含一个名为 totalPrice 的字段,其中包含 price 和 cleaning-fee 字段值的总和。在添加这些字段的值之前,以下代码将price和cleaning-fee字段的值转换为double类型。
db.createView( "listings_SearchablePrice", "listingsAndReviews", [ { "$addFields": { "totalPrice": { "$add": [ { "$ifNull": [{ "$toDouble": "$price" }, 0] }, { "$ifNull": [{ "$toDouble": "$cleaning_fee" }, 0] } ] } } } ] )
Query the listingsSearchablePrice index.
以下查询在名为listingsSearchablePrice的索引中搜索费用在100–200之间(含)的属性。
db.listingsAndReviews.aggregate([ { "$search": { "index": "listingsSearchablePrice", "range": { "path": "totalPrice", "gte": 100, "lte": 200 } } }, { "$project": { "_id": 0, "totalPrice": 1, "price": 1, "cleaning_fee": 1 } } ])
[ { price: Decimal128('80.00'), cleaning_fee: Decimal128('35.00') }, { price: Decimal128('140.00') }, { price: Decimal128('150.00') }, { price: Decimal128('121.00') }, { price: Decimal128('185.00') }, { price: Decimal128('117.00'), cleaning_fee: Decimal128('0.00') }, { price: Decimal128('135.00') }, { price: Decimal128('149.00'), cleaning_fee: Decimal128('30.00') }, { price: Decimal128('105.00') }, { price: Decimal128('100.00') }, { price: Decimal128('138.00') }, { price: Decimal128('100.00'), cleaning_fee: Decimal128('80.00') }, { price: Decimal128('112.00'), cleaning_fee: Decimal128('0.00') }, { price: Decimal128('80.00'), cleaning_fee: Decimal128('30.00') }, { price: Decimal128('129.00'), cleaning_fee: Decimal128('60.00') }, { price: Decimal128('112.00') }, { price: Decimal128('100.00'), cleaning_fee: Decimal128('100.00') }, { price: Decimal128('85.00'), cleaning_fee: Decimal128('15.00') }, { price: Decimal128('85.00'), cleaning_fee: Decimal128('40.00') }, { price: Decimal128('135.00'), cleaning_fee: Decimal128('50.00') } ] Type "it" for more
编辑视图
以下示例更新了 2000 年之前电影的 movies_ReleasedAfter2000 MongoDB 视图。
db.runCommand( { collMod: "movies_ReleasedAfter2000", viewOn: "movies", "pipeline": [ { $match: { $expr: { $lt: [ "$released", ISODate("2000-01-01T00") ] } } } ] } )
运行此命令后,MongoDB Search 会自动检测视图定义中的更改,并执行重新索引,而不会造成停机。
返回视图的管道
以下示例返回 movies_ReleasedAfter2000 视图中的管道。
db.getCollectionInfos({ name: "movies_ReleasedAfter2000" })[0].options.pipeline
[ { '$match': { '$expr': { '$gt': [ '$released', ISODate('2000-01-01T00:00:00.000Z') ] } } } ]
性能考虑因素
高度复杂的视图转换会增加索引和查询时间。这是因为mongod 在索引期间(初始同步和稳态复制)筛选和转换oplog条目时,以及在查询时将这些转换应用于返回的文档时,必须读取视图定义。
请考虑创建物化视图以避免Atlas上的额外的复制负载。您还可以直接查询源集合,以避免视图转换造成的查询延迟。
故障排除
索引更改为 FAILED
在以下场景中,索引会更改为 FAILED 状态:
您在与MongoDB搜索不兼容的视图上创建的索引。
您编辑视图的方式不符合MongoDB搜索兼容性要求。
您删除或更改视图的源集合。
例如,如果一个视图是在另一个视图上创建的,并且将父视图源更改为另一个集合。
注意
如果一个视图是其他视图的后代,这一限制也同样适用。例如,您无法更改或移除所有后代所源自的源集合。
索引更改为 STALE
在以下场景中,索引会更改为 STALE 状态:
警告
如果在视图中定义的聚合管道与集合中的文档不兼容,搜索复制将失败。例如,如果 $toDouble 表达式对包含数组的文档字段进行操作,复制将失败。确保您的视图在处理集合中的所有文档时不会出错。
如果视图定义导致聚合失败,而索引为
READY,则索引变为STALE。解析文档或更改视图定义以使其不再失败后,索引将返回到READY。当为STALE时,索引仍然可查询。如果索引脱离oplog,则会触发索引重建。如果视图定义在索引为
BUILDING时导致聚合管道失败,则在修复文档之前,则索引生成将停滞。在您解析文档或更改视图定义后,索引将返回到READY,以确保不再失败。
您可以在 Atlas 用户界面的索引状态详情页面查看索引状态。
错误:$search 仅作为管道中的第一阶段有效
当您使用 8.1 之前的MongoDB版本查询视图时,会出现此错误。
如果您使用 8.0 之前的MongoDB版本,我们建议您升级到 8.1+ 以直接查询视图。您可以升级到 8.0 来查询源集合。
如果使用MongoDB 8.0,则必须针对源集合查询视图索引。示例,对集合而不是视图运行
.aggregate()。
索引流程
当您在视图上创建MongoDB搜索索引时,mongot进程执行的任务与您在常规集合上创建MongoDB搜索索引时相同。mongot进程:
根据集合索引定义中的规则创建MongoDB Search 索引。
监控变更流以了解您定义了MongoDB Search 索引的集合的文档和索引的当前状态。
处理MongoDB搜索查询,并将匹配文档的文档ID 和其他搜索元数据返回给
mongod,后者执行完整文档查找并将结果返回给客户端。
当您在视图上创建MongoDB搜索索引时,会在步骤 1 和 2 期间应用视图定义,并且转换后的文档会根据搜索索引定义进行索引,然后存储在磁盘上。
了解详情
要了解有关视图的更多信息,请参阅视图。
要在视图上创建MongoDB 向量搜索索引,请参阅将视图与MongoDB 向量搜索 结合使用。