如果您在集合的 索引定义 中配置了 StoredSource 选项,则MongoDB Search 会将指定字段存储在mongot 上。您可以在MongoDB搜索查询中使用 returnStoredSource 布尔选项,仅检索这些字段,而不是从集合中获取完整文档。
By default, MongoDB Search performs full document lookup implicitly on the backend database after MongoDB Search matches documents for the query. This lookup could significantly degrade performance for subsequent aggregation pipeline stages that fetch all matched dataset from $search stage (such as $sort, $group) or filter a large part of it (such as $match, $skip). If you configured the storedSource option, you can use the returnStoredSource option to retrieve only parts of the documents directly stored on MongoDB Search and avoid full document lookup on the database. This allows you to perform most database-side filtering operations on documents with a minimum number of fields. You can then retrieve all the fields from the documents at a later stage in the pipeline using $lookup.
注意
returnStoredSource仅适用于运行MongoDB 7.0 及更高版本的集群。For sharded collections,
$lookupis only available on clusters running MongoDB 5.1 and later.
语法
returnStoredSource 查询中包含以下语法:
{ $search: { "<operator>": { <operator-specification> }, "returnStoredSource": true | false // optional, defaults to "false" } }
要了解有关查询语法的更多信息,请参阅$search 。
行为
returnStoredSource 布尔选项指定MongoDB Search 是否必须对数据库执行完整文档查找,还是直接从MongoDB Search 返回存储的字段。仅当索引定义包含在MongoDB Search 上存储字段的配置时,才能使用 returnStoredSource 选项。要学习;了解有关在MongoDB Search 上存储字段的更多信息,请参阅索引参考资料和在MongoDB Search 索引中定义存储的源字段。
您可以为returnStoredSource选项设置以下值之一:
true- 仅返回直接从MongoDB Search 中存储的源字段false— 对后端数据库执行隐式完整文档查找(默认)
如果在将 returnStoredSource 布尔选项设立为 true 的情况下运行MongoDB Search 查询:
如果文档不包含配置用于存储的字段,则MongoDB Search 返回空文档。
如果索引定义不包含“存储的源”配置, MongoDB Search 将返回错误。
由于复制延迟, MongoDB Search 可能会返回过时的数据。
MongoDB Search 可能会返回分片的集群的重复数据。
If you perform a high volume and rate of data insert and update operations for your collection on the backend database, MongoDB Search might return stale data because the data stored on mongot might not be current due to a replication lag. You can view the approximate number of milliseconds that MongoDB Search is behind in replicating changes from the oplog of mongod in the Atlas UI. To learn more, see Review MongoDB Search Metrics.
如果在数据块迁移过程中出现孤立文档, MongoDB Search 可能会为针对分片集群的查询返回重复文档。
使用示例
如果您的$search阶段丢弃了大量结果,而您需要对数据库执行隐式文档查找,我们建议使用returnStoredSource选项。 您可以存储排序或筛选所需的字段,并在查询时使用returnStoredSource选项来执行以下操作:
对MongoDB Search 返回的部分文档进行中间操作
$lookup如果需要完整的文档,则在管道末端。
重要
为了效率,请仅在MongoDB Search 上配置最少数量的存储字段。如果您的文档大到足以在查找期间导致问题,请使用此选项。
示例
本节中的示例使用 sample_mflix.movies集合。这些示例展示了如何对MongoDB搜索在 $search 阶段之后返回的文档进行排序或匹配,然后在数据库中查找文档。
使用以下索引定义创建索引。 collection的索引定义指定以下字段:
索引
title字段存储
year和title字段
{ "mappings": { "fields": { "title": { "type": "string" } } }, "storedSource": { "include": [ "year", "title" ] } } 对collection运行以下查询:
db.movies.aggregate([ { // search and output documents $search: { "text": { "query": "baseball", "path": "title" }, "returnStoredSource": true // return stored fields only } }, // fetch all matched dataset from $search stage and sort it { $sort: {"year": 1, "title": 1} }, // discard everything except top 10 results { $limit: 10 }, // perform full document lookup for top 10 documents only { $lookup: { from: "movies", localField: "_id", foreignField: "_id", as: "document" } } ]) 该查询返回以下文档:
1 [ 2 { 3 _id: ObjectId("573a1399f29313caabced370"), 4 title: 'Mr. Baseball', 5 year: 1992, 6 document: [ 7 { ... } // full document returned by $lookup 8 ] 9 }, 10 { 11 _id: ObjectId("573a1399f29313caabcee1aa"), 12 title: 'Baseball', 13 year: 1994, 14 document: [ 15 { ... } // full document returned by $lookup 16 ] 17 } 18 ]
使用以下索引定义创建索引。 collection的索引定义指定以下字段:
索引
title字段存储
imdb.rating和imdb.votes字段
{ "mappings": { "fields": { "title": { "type": "string" } } }, "storedSource": { "include": [ "imdb.rating", "imdb.votes" ] } } 对collection运行以下查询:
db.movies.aggregate([ { // search and output documents $search: { "text": { "query": "baseball", "path": "title" }, "returnStoredSource": true // return stored fields only } }, // filter dataset from $search stage using $match { $match: {$or: [ { "imdb.rating": { $gt: 8.2 } }, { "imdb.votes": { $gte: 4500 } } ]} }, // perform full document lookup for matched documents only { $lookup: { from: "movies", localField: "_id", foreignField: "_id", as: "document" } } ]) 该查询返回以下文档:
1 [ 2 { 3 _id: ObjectId("573a1399f29313caabcee1aa"), 4 imdb: { rating: 9.1, votes: 2460 }, 5 document: [ 6 { ... } // full document returned by $lookup 7 ] 8 }, 9 { 10 _id: ObjectId("573a1399f29313caabced370"), 11 imdb: { rating: 5.8, votes: 7617 }, 12 document: [ 13 { ... } // full document returned by $lookup 14 ] 15 } 16 ]