Docs Menu
Docs Home
/
Atlas
/ / / /

queryString 演算子

queryString

queryString演算子は、インデックス付きフィールドと値の組み合わせのクエリをサポートしています。 queryString演算子を使用して、 stringフィールドでテキスト、ワイルドカード、正規表現、ファジー、範囲検索を実行できます。

queryString の構文は次のとおりです。

1{
2 "$search": {
3 "index": <index name>, // optional, defaults to "default"
4 "queryString": {
5 "defaultPath": "<default-field-to-search>",
6 "query": "(<field-to-search>: (<search-values>) AND|OR (<search-values>)) AND|OR (<search-values>)"
7 }
8 }
9}

queryString では、次の用語を使用してクエリを作成します。

フィールド
タイプ
説明
必要性

defaultPath

string

デフォルトで検索するインデックス付きフィールド。 Atlas Search では、 フィールドを省略してqueryで検索する場合のみ、 defaultPathのフィールドが検索されます。

はい

query

string

検索する 1 つ以上のインデックス付きフィールドと値。 フィールドと値はコロンで区切られます。 たとえば、 plotフィールドで string baseballを検索するには、 plot:baseballを使用します。 複数の検索条件を組み合わせるには、次の演算子を使用できます。

次のように、フィールドと値を組み合わせることができます。

AND

ブール値演算子がANDことを示します。 結果にドキュメントを含めるには、すべての検索値が存在する必要があります。

OR

ブール値演算子がORことを示します。 結果に含めるドキュメントには、検索値の少なくとも 1 つが存在する必要があります。

NOT

ブール値演算子がNOTことを示します。 結果にドキュメントを含めるには、指定された検索値が存在しない必要があります。

TO

検索する範囲を示します。 包括的範囲には[] 、排他的範囲には{} 、または半分開いた範囲には{][}を使用できます。 結果に含めるドキュメントには、指定した範囲内にある値が存在する必要があります。

()

サブクエリの区切り文字。 検索するフィールドと値をグループ化するには、括弧 を使用します。

次の方法を使用して、ワイルドカードおよび正規式のクエリを実行できます。

?

一致する任意の 1 文字を示します。

*

一致する 0 文字以上の文字を示します。

queryString演算子は、 *をクエリの最初の文字として含むワイルドカード クエリをサポートしていません。 queryString演算子は、 *の前にある文字をプレフィックスとして、または完全に一致する必要がある文字として扱います。

/

正規表現の区切り文字。

~

指定された編集回数まで、検索タームに類似する文字列を検索するためのファジー検索を示します。 たとえば、 { "query":"foo":"stop"~2 }は次の値と等しいです。

{ "defaultPath": "foo", "query": "stop", "maxEdits": 2 }

queryString演算子は、最大2編集のファジー検索のみをサポートします。 結果が多くの辞書タームと一致する可能性があるため、距離が長いと有用ではありません。

stringに複数のタームとともに ~ を使用する場合、queryString 演算子は、指定されたターム数内のタームを近接検索します。 たとえば、 { "query":"foo":"New York"~2 }は次の値と等しいです。

{ "defaultPath": "foo", "query": "New York", "slop": 2 }

はい

score

オブジェクト

一致する検索結果に割り当てられたスコア。 デフォルトのスコアは、次のオプションを使用して変更できます。

  • boost: 結果のスコアに指定された数値を掛けます。

  • constant: 結果のスコアを指定された数値に置き換えます。

  • function: 結果のスコアを指定された式で置き換えます。

配列の値をクエリする際、Atlas Search は、配列内でクエリに一致する値の数に関係なく、同一のスコアを割り当てます。

クエリでscoreを使用する方法については、「 結果内のドキュメントにスコアを付ける 」を参照してください。

no

次の例では、sample_mflix データベースの movies コレクションを使用します。クラスターにサンプル データセットがある場合は、動的マッピングを使用してdefault という名前の Atlas Search インデックスを作成し、クラスターでサンプル クエリを実行できます。

次の例では、queryString 演算子を使用して、タイトルが Rocky で、movies コレクションのパス title にある IV4、または Four のいずれかを持つ映画をクエリします。次のクエリでは、defaultPath にフィールドが指定されていますが、query にはフィールドが指定されていません。クエリには、title 以外のすべてのフィールドを除外する $project ステージが含まれています。

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "Rocky AND (IV OR 4 OR Four)"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1
14 }
15 }
16])

このクエリでは、 queryにフィールドがないため、Atlas Search はdefaultPathで検索を実行します。 これにより、次の結果が返されます。

{ "title" : "Rocky IV" }

次の例では、queryString 演算子を使用して、タイトルが Rocky であり、2345 のいずれの形式の数値も含まない映画を、movies コレクションの title パスでクエリします。次のクエリでは、defaultPath にフィールドが指定されていますが、query にはフィールドが指定されていません。クエリには、title 以外のすべてのフィールドを除外する $project ステージが含まれています。

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "Rocky AND NOT ((II OR 2 OR Two) OR (III OR 3 OR Three) OR (IV OR 4 OR Four) OR (V OR 5 OR Five))"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1
14 }
15 }
16])
[
{ title: 'Rocky' },
{ title: 'Rocky Marciano' },
{ title: 'Rocky Balboa' },
{ title: 'The Rocky Horror Picture Show' },
{ title: 'The Adventures of Rocky & Bullwinkle' }
]

次の例では、queryString 演算子を使用して、Drama ジャンル内のタイトルが The Italian の映画のフィールドの組み合わせをクエリします。defaultPathplot フィールドは、query フィールドで指定されたどちらのフィールドにも一致する結果が見つからない場合にのみ検索されます。query フィールドには、検索するフレーズの区切りとして、エスケープされた二重引用符が含まれています。

クエリには、次の操作を行うための$projectステージが含まれています。

  • すべてのフィールドを除外_idtitleplotgenres

  • 次のフィールドを追加: score

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:\"The Italian\" AND genres:Drama"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 1,
13 "title": 1,
14 "plot": 1,
15 "genres": 1,
16 score: { $meta: "searchScore" }
17 }
18 }
19])
{
"_id" : ObjectId("573a1390f29313caabcd56df"),
"plot" : "An immigrant leaves his sweetheart in Italy to find a better life across the sea in the grimy slums of New York. They are eventually reunited and marry. But life in New York is hard and ...",
"genres" : [ "Drama" ],
"title" : "The Italian",
"score" : 4.975106716156006
}
{
"_id" : ObjectId("573a13b3f29313caabd3e36c"),
"plot" : "Set in 2002, an abandoned 5-year-old boy living in a rundown orphanage in a small Russian village is adopted by an Italian family.",
"genres" : [ "Drama" ],
"title" : "The Italian",
"score" : 4.975106716156006
}
{
"_id" : ObjectId("573a13c7f29313caabd756ee"),
"plot" : "A romantic fairy tale about a 19-year old orphan girl who, as her sole inheritance, gets an antique key that unlocks both an old Italian villa and the secrets of her family history.",
"genres" : [ "Comedy", "Drama", "Romance" ],
"title" : "The Italian Key",
"score" : 4.221206188201904
}
{
"_id" : ObjectId("573a13caf29313caabd7d1e4"),
"plot" : "A chronicle of the 1969 bombing at a major national bank in Milan and its aftermath.",
"genres" : [ "Drama" ],
"title" : "Piazza Fontana: The Italian Conspiracy",
"score" : 3.4441356658935547
}

次の例では、queryString 演算子を使用して、plot フィールドでcaptain またはkirk およびenterprise の用語の組み合わせをクエリします。出力を 3 件の結果に制限する $limit ステージと次のための $project ステージが含まれています。

  • すべてのフィールドを除外titleplotfullplot

  • 次のフィールドを追加: score

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:(captain OR kirk) AND enterprise"
7 }
8 }
9 },
10 {
11 $limit: 3
12 },
13 {
14 $project: {
15 "_id": 0,
16 "title": 1,
17 "plot": 1,
18 "fullplot": 1,
19 score: { $meta: "searchScore" }
20 }
21 }
22])
{
"plot" : "Captain Picard, with the help of supposedly dead Captain Kirk, must stop a madman willing to murder on a planetary scale in order to enter an energy ribbon.",
"fullplot" : "In the late 23rd century, the gala maiden voyage of the third Starship Enterprise (NCC-1701-B) boasts such luminaries as Pavel Chekov, Montgomery Scott and the legendary Captain James T. Kirk as guests. But the maiden voyage turns to disaster as the unprepared ship is forced to rescue two transport ships from a mysterious energy ribbon. The Enterprise manages to save a handful of the ships' passengers and barely makes it out intact... but at the cost of Captain Kirk's life. Seventy-eight years later, Captain Jean-Luc Picard and the crew of the Enterprise-D find themselves at odds with the renegade scientist Tolian Soran... who is destroying entire star systems. Only one man can help Picard stop Soran's scheme... and he's been dead for seventy-eight years.",
"title" : "Star Trek: Generations",
"score" : 11.274821281433105
}
{
"plot" : "Captain Kirk and his crew must deal with Mr. Spock's long-lost half-brother who hijacks the Enterprise for an obsessive search for God at the center of the galaxy.",
"fullplot" : "When the newly-christened starship Enterprise's shakedown cruise goes poorly, Captain Kirk and crew put her into Spacedock for repairs. But an urgent mission interrupts their Earth-bound shore leave. A renegade Vulcan named Sybok has taken three ambassadors hostage on Nimbus III, the Planet of Galactic Peace. This event also attracts the attention of a Klingon captain who wants to make a name for himself and sets out to pursue the Enterprise. Sybok's ragtag army captures the Enterprise and takes her on a journey to the center of the galaxy in search of the Supreme Being.",
"title" : "Star Trek V: The Final Frontier",
"score" : 9.889547348022461
}
{
"plot" : "When an alien spacecraft of enormous power is spotted approaching Earth, Admiral Kirk resumes command of the Starship Enterprise in order to intercept, examine and hopefully stop the intruder.",
"fullplot" : "A massive alien spacecraft of enormous power is approaching Earth, destroying everything in its path. The only star ship in range is the USS Enterprise still in dry-dock after a major overhaul. As Captain Willard Decker readies his ship and his crew to face this menace, Admiral James T. Kirk arrives with orders to take command of the Enterprise and intercept the alien intruder. But it has been three years since Kirk last commanded the Enterprise on its historic five year mission... is he up to the task of saving the Earth?",
"title" : "Star Trek: The Motion Picture",
"score" : 8.322310447692871
}

結果のドキュメントは、次の理由で一致します。

  • 最初のドキュメントでは、 queryごとに検索するフィールドであるplotフィールドにcaptainkirkの両方が含まれていますが、一致の条件を満たすにはどちらか 1 つだけが必要です。 enterpriseというタームについて、Atlas Search はfullplotフィールド( plotフィールドにenterpriseが含まれていないためdefaultPath )を検索し、 fullplotフィールドでenterpriseを見つけます。

  • 2 番目のドキュメントでは、 plotフィールドに 3 つの検索タームがすべて含まれています。

  • 3 番目のドキュメントでは、 plotフィールドにkirkenterpriseが含まれています。

このセクションの例では、queryString 演算子を使用して、captainkirkchess という用語を含む映画のプロットをクエリします。この例では、同じ検索語を括弧でグループ化する方法の違いによって、検索結果に含まれるドキュメントがどのように異なるかを示しています。

クエリには、次の操作を行うための$projectステージも含まれています。

  • すべてのフィールドを除外titleplotfullpath

  • 次のフィールドを追加: score

次のクエリは、 plotフィールドでchesscaptainまたはkirkのいずれかを検索します。

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:(captain OR kirk) AND chess"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1,
14 "plot": 1,
15 "fullplot": 1,
16 score: { $meta: "searchScore" }
17 }
18 }
19])
{
"fullplot" : "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew.",
"plot" : "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction.",
"title" : "Star Trek Into Darkness",
"score" : 7.968792915344238
}

plot には captainkirk という用語が含まれていますが、一致条件を満たすために必要なのは 1 つだけで、plot にはありませんが、defaultPathfullplot には chess という用語が表示されるため、結果のドキュメントは一致します。

次のクエリは、 plotフィールドでcaptainまたはkirkchessの両方を検索します。

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:captain OR (kirk AND chess)"
7 }
8 }
9 },
10 {
11 $limit: 5
12 },
13 {
14 $project: {
15 "_id": 0,
16 "title": 1,
17 "plot": 1,
18 "fullplot": 1,
19 score: { $meta: "searchScore" }
20 }
21 }
22])
{
"fullplot" : "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew.",
"plot" : "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction.",
"title" : "Star Trek Into Darkness",
"score" : 9.227973937988281
}
{
"plot" : "Captain Picard, with the help of supposedly dead Captain Kirk, must stop a madman willing to murder on a planetary scale in order to enter an energy ribbon.",
"title" : "Star Trek: Generations",
"fullplot" : "In the late 23rd century, the gala maiden voyage of the third Starship Enterprise (NCC-1701-B) boasts such luminaries as Pavel Chekov, Montgomery Scott and the legendary Captain James T. Kirk as guests. But the maiden voyage turns to disaster as the unprepared ship is forced to rescue two transport ships from a mysterious energy ribbon. The Enterprise manages to save a handful of the ships' passengers and barely makes it out intact... but at the cost of Captain Kirk's life. Seventy-eight years later, Captain Jean-Luc Picard and the crew of the Enterprise-D find themselves at odds with the renegade scientist Tolian Soran... who is destroying entire star systems. Only one man can help Picard stop Soran's scheme... and he's been dead for seventy-eight years.",
"score" : 3.3556222915649414
}
{
"plot" : "An army cadet accompanies an irascible, blind captain on a week-long trip from Turin to Naples. The captain, Fausto, who wants no pity, brooks no disagreement, and charges into every ...",
"title" : "Scent of a Woman",
"fullplot" : "An army cadet accompanies an irascible, blind captain on a week-long trip from Turin to Naples. The captain, Fausto, who wants no pity, brooks no disagreement, and charges into every situation, nicknames the youth Ciccio (\"Babyfat\"), and spends the next few days ordering him about and generally behaving badly in public. In Rome, Fausto summons a priest to ask for his blessing; in Naples, where Fausto joins a blind lieutenant for drinking and revelry, the two soldiers talk quietly and seriously about \"going through with it.\" Also in Naples is Sara, in love with Fausto, but treated cruelly by him. What do the blind soldiers plan? Can Sara soften Fausto's hardened heart?",
"score" : 3.2553727626800537
}
{
"plot" : "A seductive woman falls in love with a mysterious ship's captain.",
"title" : "Pandora and the Flying Dutchman",
"fullplot" : "Albert Lewin's interpretation of the legend of the Flying Dutchman. In a little Spanish seaport named Esperanza, during the 30s, appears Hendrick van der Zee, the mysterious captain of a yacht (he is the only one aboard). Pandora is a beautiful woman (who men kill and die for). She's never really fallen in love with any man, but she feels very attracted to Hendrick... We are soon taught that Hendrick is the Flying Dutchman, this sailor of the 17th century that has been cursed by God to wander over the seas until the Doomsday... unless a woman is ready to die for him...",
"score" : 3.2536067962646484
}
{
"plot" : "The adventures of pirate Captain Red and his first mate Frog.",
"title" : "Pirates",
"fullplot" : "Captain Red runs a hardy pirate ship with the able assistance of Frog, a dashing young French sailor. One day Capt. Red is captured and taken aboard a Spanish galleon, but thanks to his inventiveness, he raises the crew to mutiny, takes over the ship, and kidnaps the niece of the governor of Maracaibo. The question is, can he keep this pace up?",
"score" : 3.2536067962646484
}

plot フィールドに用語captain が含まれているため、結果のドキュメントが一致します。Atlas Search は fullplot フィールドでkirkchess という用語も見つけるため、最初のドキュメントのスコアが高くなります。ただし、一致条件を満たすためにこれらの用語が存在する必要はありません。

次の例では、 queryString演算子を使用して、 countから任意の文字(ワイルドカード文字*を使用して定義)までのテキスト値の範囲を、アルファベットの昇順で映画のタイトルに対してクエリします。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:[count TO *]"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Blacksmith Scene' },
3 { title: 'The Great Train Robbery' },
4 { title: 'The Land Beyond the Sunset' },
5 { title: 'A Corner in Wheat' },
6 { title: 'Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics' },
7 { title: 'Traffic in Souls' },
8 { title: 'Gertie the Dinosaur' },
9 { title: 'In the Land of the Head Hunters' },
10 { title: 'The Perils of Pauline' },
11 { title: 'The Birth of a Nation' }
12]

Atlas Search 結果には、 Blacksmith SceneA Corner in Wheatというタイトルの映画が含まれますが、クエリの範囲はcountの文字から始まります。 デフォルトのアナライザlucene.standardでインデックスを作成すると、Atlas Search はtitleフィールドのターム用に個別のトークンを作成し、クエリ用語を個々のトークンと照合します。 具体的には、Atlas Search は次のトークンを作成します。このトークンの少なくとも 1 つ(ルート で示)がクエリ条件に一致します。

タイトル
標準アナライザ トークン

ブラックメソッド フィールド

blacksmith, sceneルート

お茶の特権

a, corner, in, wheat

lucene.keywordアナライザを使用してインデックスを作成すると、Atlas Search はtitleフィールドの string 全体に対して単一のトークンを作成し、同じクエリに対して次のような結果が得られます。

[
{ title: 'è Nous la Libertè' },
{ title: 'tom thumb' },
{ title: 'è nos amours' },
{ title: 'èke och hans vèrld' },
{ title: 'èdipussi' },
{ title: 's/y Glèdjen' },
{ title: 'èAy, Carmela!' },
{ title: 'èlisa' },
{ title: 'èxtasis' },
{ title: 'eXistenZ' }
]

次の例では、 queryString演算子を使用して、 dramaの映画ジャンルとmanからmenまでのテキスト値の範囲のタイトルを、アルファベットの昇順でクエリします。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:[man TO men] AND genres: Drama"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1,
17 "genres": 1
18 }
19 }
20])
1[
2 { genres: [ 'Drama' ], title: 'The Wedding March' },
3 { genres: [ 'Drama' ], title: 'Maria Chapdelaine' },
4 { genres: [ 'Drama' ], title: 'Of Mice and Men' },
5 { genres: [ 'Drama' ], title: 'All the King's Men' },
6 { genres: [ 'Drama' ], title: 'Wuya yu maque' },
7 { genres: [ 'Drama' ], title: 'The Men' },
8 { genres: [ 'Drama' ], title: 'The Member of the Wedding' },
9 { genres: [ 'Drama' ], title: 'The Great Man' },
10 { genres: [ 'Drama' ], title: 'A Matter of Dignity' },
11 { genres: [ 'Drama' ], title: 'The Last Angry Man' }
12]

結果のドキュメントは、 genresフィールドにdramaというタームが含まれ、映画titleフィールドにはアルファベット順にManMenの間のタームが含まれているため、一致します( ManMaqueMarchMariaMatterMemberMenの結果が含まれます)。

次の例では、 queryString演算子を使用して、ファジー、ワイルドカード、正規表現を使用して映画タイトルをクエリします。 クエリには、 title以外のすべてのフィールドを除外する$projectステージが含まれています。

次のクエリでは、ファジー( ~ )を使用して、 titleフィールドで、最大 2 文字のバリエーションを持つcatchに類似するタームを含む映画タイトルをファジー検索します。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "catch~2"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Catch-22' },
3 { title: 'Catch That Girl' },
4 { title: 'Catch That Kid' },
5 { title: 'Catch a Fire' },
6 { title: 'Catch Me Daddy' },
7 { title: 'Death Watch' },
8 { title: 'Patch Adams' },
9 { title: "Batch '81" },
10 { title: 'Briar Patch' },
11 { title: 'Night Watch' }
12]

次のクエリは、ワイルドカード式を使用して、 titleフィールドで、文字cou*t?*を含む映画のタイトルを検索します。ここで、 *は追加の文字数を、 ?は任意の 1 文字を示します。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "cou*t?*"
7 }
8 }
9 },
10 {
11 "$limit": 5
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'A Day in the Country' },
3 { title: 'Diary of a Country Priest' },
4 { title: 'Cry, the Beloved Country' },
5 { title: 'The Country Girl' },
6 { title: 'Raintree County' }
7]

結果内のドキュメントが一致するのは、 titleフィールドにCountryまたはCountyという用語が含まれているためです。これらはcouで始まり、その後に任意の数の文字が続くクエリ条件(結果ではn )と次に、 tに 1 つまたは複数の文字が続く( countryの場合はr 、または結果のcountyyのように)。

次のクエリは正規表現を使用して、title フィールドで、任意の文字とその後に文字tal で始まり、y ianの後に またはtal のいずれかを含む映画タイトルを検索します。

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "/.tal(y|ian)/"
7 }
8 }
9 },
10 {
11 "$limit": 5
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'The Italian' },
3 { title: 'Journey to Italy' },
4 { title: 'Divorce Italian Style' },
5 { title: 'Marriage Italian Style' },
6 { title: 'Jealousy, Italian Style' }
7]

結果内のドキュメントが一致するのは、 titleフィールドにItalyまたはItalianというタームが含まれているためです。これらは任意の文字(結果のI )のクエリ条件に一致し、その後にtal y (結果のItalyのように)またはian (結果のItalianのように)。

次のクエリは、movies コレクションの title フィールドで MarvelAvengersIron Man、または Captain America を検索します。

次のクエリは、検索条件に一致する次のバケット(年)の映画を返します。

  • 2000 (このバケットの下限値を含む)

  • 2005: 2000 バケットの 排他的上限であり、このバケットの下限

  • 2010: 2005 バケットの 排他的上限であり、このバケットの下限

  • 2015: 2010 バケットの 排他的上限であり、このバケットの下限

  • 2020: 2015 の排他的上限

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "queryString": {
7 "defaultPath": "title",
8 "query": "\"Marvel\" OR \"Avengers\" OR \"Iron Man\" OR \"Captain America\""
9 }
10 },
11 "facets": {
12 "yearFacet": {
13 "type": "number",
14 "path": "year",
15 "boundaries": [1990, 2000, 2005, 2010, 2015, 2020 ]
16 }
17 }
18 }
19 }
20 }
21])
[
{
count: { lowerBound: Long('16') },
facet: {
yearFacet: {
buckets: [
{ _id: 1990, count: Long('1') },
{ _id: 2000, count: Long('0') },
{ _id: 2005, count: Long('4') },
{ _id: 2010, count: Long('10') },
{ _id: 2015, count: Long('1') }
]
}
}
}
]

次のクエリは、検索条件に一致し、次のバケット(年)に含まれる映画を返します。

  • 2000 (このバケットの下限値を含む)

  • 2005: 2000 バケットの 排他的上限であり、このバケットの下限

  • 2010: 2005 バケットの 排他的上限であり、このバケットの下限

  • 2015: 2010 バケットの 排他的上限であり、このバケットの下限

  • 2020: 2015 の排他的上限

1db.movies.aggregate([
2 {
3 "$search": {
4 "facet": {
5 "operator": {
6 "queryString": {
7 "defaultPath": "title",
8 "query": "\"Marvel\" OR \"Avengers\" OR \"Iron Man\" OR \"Captain America\""
9 }
10 },
11 "facets": {
12 "yearFacet": {
13 "type": "number",
14 "path": "year",
15 "boundaries": [1990, 2000, 2005, 2010, 2015, 2020 ]
16 }
17 }
18 }
19 }
20 },
21 {
22 "$facet": {
23 "docs": [
24 { "$project":
25 {
26 "title": 1,
27 "year": 1
28 }
29 }
30 ],
31 "meta": [
32 {"$replaceWith": "$$SEARCH_META"},
33 {"$limit": 1}
34 ]
35 }
36 },
37 {
38 "$set": {
39 "meta": {
40 "$arrayElemAt": ["$meta", 0]
41 }
42 }
43 }
44])
[
{
docs: [
{
_id: ObjectId('573a13f3f29313caabddecf8'),
title: 'Iron Man and Captain America: Heroes United',
year: 2014
},
{
_id: ObjectId('573a13adf29313caabd2bf71'),
title: 'Iron Man',
year: 2008
},
{
_id: ObjectId('573a13c1f29313caabd659d3'),
title: 'Iron Man 2',
year: 2010
},
{
_id: ObjectId('573a13c3f29313caabd69c74'),
title: 'Iron Man 3',
year: 2013
},
{
_id: ObjectId('573a13b4f29313caabd4011f'),
title: 'Captain America: The First Avenger',
year: 2011
},
{
_id: ObjectId('573a13d2f29313caabd91d33'),
year: 2014,
title: 'Captain America: The Winter Soldier'
},
{
_id: ObjectId('573a13bbf29313caabd53752'),
title: 'The Invincible Iron Man',
year: 2007
},
{
_id: ObjectId('573a139af29313caabcf075d'),
title: 'The Avengers',
year: 1998
},
{
_id: ObjectId('573a13b6f29313caabd47da7'),
title: 'Ultimate Avengers',
year: 2006
},
{
_id: ObjectId('573a13baf29313caabd506e4'),
year: 2012,
title: 'The Avengers'
},
{
_id: ObjectId('573a13e0f29313caabdba876'),
title: 'Iron Man: Rise of Technovore',
year: 2013
},
{
_id: ObjectId('573a13b8f29313caabd4ca34'),
title: 'Ultimate Avengers II',
year: 2006
},
{
_id: ObjectId('573a13dcf29313caabdb2759'),
title: 'Avengers: Age of Ultron',
year: 2015
},
{
_id: ObjectId('573a13d9f29313caabda9807'),
title: 'Marvel One-Shot: Item 47',
year: 2012
},
{
_id: ObjectId('573a13e6f29313caabdc59ff'),
title: 'Marvel One-Shot: Agent Carter',
year: 2013
},
{
_id: ObjectId('573a13ecf29313caabdd271a'),
title: 'Avengers Confidential: Black Widow & Punisher',
year: 2014
}
],
meta: {
count: { lowerBound: Long('16') },
facet: {
yearFacet: {
buckets: [
{ _id: 1990, count: Long('1') },
{ _id: 2000, count: Long('0') },
{ _id: 2005, count: Long('4') },
{ _id: 2010, count: Long('10') },
{ _id: 2015, count: Long('1') }
]
}
}
}
}
]

戻る

phrase

項目一覧