コレクションの インデックス定義 で storedSource オプションを構成した場合、 MongoDB Search は指定されたフィールドをmongot に保存します。MongoDB Search クエリで 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 から保存されたフィールドを直接返す必要があるかを指定します。returnStoredSource オプションは、インデックス定義にMongoDB Search にフィールドを保存するための構成が含まれている場合にのみ使用できます。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 が $search ステージの後に返すドキュメントに対してソートまたは一致を実行し、その後データベース上のドキュメントを検索する方法を示しています。
以下のインデックス定義を使用してインデックスを作成します。 コレクションのインデックス定義では、次のフィールドを指定します。
インデックス
titleフィールドyearフィールドとtitleフィールドを保存する
{ "mappings": { "fields": { "title": { "type": "string" } } }, "storedSource": { "include": [ "year", "title" ] } } コレクションに対して次のクエリを実行します。
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 ]
以下のインデックス定義を使用してインデックスを作成します。 コレクションのインデックス定義では、次のフィールドを指定します。
インデックス
titleフィールドimdb.ratingフィールドとimdb.votesフィールドを保存する
{ "mappings": { "fields": { "title": { "type": "string" } } }, "storedSource": { "include": [ "imdb.rating", "imdb.votes" ] } } コレクションに対して次のクエリを実行します。
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 ]