AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

非アルファベットデータを文字列として検索する方法

このチュートリアルでは、フィールドを文字列に変換し、マテリアライズドビューに保存することで、非文字列フィールドに対して文字列特有のクエリを実行する方法を示します。マテリアライズドビューを使用すると、文字列専用の演算子を用いて変換されたフィールドをクエリし、元のデータをソース コレクションにそのまま保持できます。

このチュートリアルでは、次の手順について説明します。

開始する前に、クラスターが前提条件に記載されている要件を満たしていることを確認してください。

This section demonstrates how to create a materialized view named airbnb-mat-view on the sample_airbnb.listingsAndReviews collection. This view stores various numeric and date fields from the source collection as string fields.

To run the queries in the Run Queries on the Converted Fields stage of this tutorial, you need to create MongoDB Search indexes on the converted string fields in your airbnb_mat_view materialized view.

The following JSON definitions define MongoDB Search indexes on the airbnb_mat_view materialized view. You can use dynamic or static mappings to specify which fields you want to index in your collection, depending on the type of queries you want to run. To learn more about field mappings or MongoDB Search index syntax, see Define Field Mappings for a MongoDB Search Index or Index Reference, respectively.

次のJSON MongoDB Searchインデックス定義では、動的マッピングを使用してマテリアライズドビュー内のフィールドにインデックス。このインデックスを使用して、queryString 演算子を使用してクエリを実行できます。

動的にインデックスされたフィールドに対して オートコンプリート 演算子を使用してクエリを実行することはできません。

{
"mappings": {
"dynamic": true
}
}

次のJSON MongoDB Searchインデックス定義では、静的マッピングを使用してマテリアライズドビュー内のフィールドをオートコンプリートタイプとしてインデックス。このインデックスを使用して、オートコンプリート演算子を使用してクエリを実行できます。

タイプ autocomplete としてインデックス付けされたフィールドに対して queryString 演算子を使用するクエリは実行できません。

{
"mappings": {
"dynamic": false,
"fields": {
"accommodatesNumber": [
{
"dynamic": true,
"type": "document"
},
{
"minGrams": 1,
"type": "autocomplete"
}
],
"lastScrapedDate": [
{
"dynamic": true,
"type": "document"
},
{
"type": "autocomplete"
}
],
"maximumNumberOfNights": [
{
"dynamic": true,
"type": "document"
},
{
"minGrams": 1,
"type": "autocomplete"
}
]
}
}
}

希望するインターフェースを使用して、上記で定義されたインデックスを作成する方法については、サポートされているクライアントを参照してください。

string に変換された数値フィールドと日付フィールドに対してクエリを実行できます。 このチュートリアルでは、 queryStringオートコンプリート演算子を使用してプロパティを検索します。 クエリは、次のパイプライン ステージを使用します。

  • $search コレクションを検索するステージ

  • $limit stage to limit the output to 5 results

  • $project 除外するステージ _id

このセクションでは、クラスターに接続し、 airbnb_mat_viewコレクションのフィールドに対して 演算子を使用してサンプルクエリを実行します。

注意

マテリアライズドビューで文字列に変換された日付および数値フィールドに対して、近くのクエリまたは範囲のクエリを実行することはできません。

The following procedure runs queryString queries against the converted fields. Use the index that you created with dynamic mappings.

The following procedure runs autocomplete queries against the converted fields. Use the index that you created with static mappings.