For AI agents: a documentation index is available at https://www.mongodb.com/ja-jp/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

near (MongoDB Search 演算子)

MongoDB Searchインデックスがある場合でも、near 演算子を使用して配列に保存されている数値または日付値をクエリすることはできません。範囲は、配列内のインデックス付き数値または日付値をクエリする目的でのみ使用できます。

near

near演算子は、数値、日付、GeoJSON ポイント値のクエリとスコアリングをサポートしています。 この演算子は、以下の検索を実行するために使用できます。

  • BSON int32int64doubleデータ型の数値フィールド。

  • ISODate形式の BSON date型の日付フィールド。

  • 緯度と経度の座標を使用して定義される地理的ロケーション フィールド。

near 演算子を使用すると、数値または日付に近い結果を見つけることができます。near 演算子は、数値または日付に近接することでMongoDB Search 結果をスコア付けします。

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

{
$search: {
"index": <index name>, // optional, defaults to "default"
"near": {
"path": "<field-to-search>",
"origin": <date-or-number>,
"pivot": <pivot-distance>,
"score": <score-options>
}
}
}

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

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

origin

日付、数値、または地理的

近くの検索する数値、日付、または地理的ポイント。 これは、結果の近接性が測定されるオリジンです。

はい

path

文字列または複数の文字列の配列

インデックス付きフィールドまたは検索するフィールド。

はい

pivot

数値

MongoDB Search 結果ドキュメントのスコアを計算するために使用する値。スコアは、次の式を使用して計算されます。

pivot
score = ------------------
pivot + distance

上記で、 distanceoriginとインデックス付きフィールド値の差です。

結果のインデックス フィールド値がoriginからpivot単位離れている場合、スコアは1/2 (または0.5 )と等しくなります。 pivotの値は より大きくなければなりません( >0

originが の場合:

  • 数値、 pivotは整数または浮動小数点数として指定できます。

  • 日付、 pivotはミリ秒単位で指定する必要があり、 32または64ビット整数として指定できます。 例:

    • 1 分は次と等しい: 60,000 ms

    • 1 時間は に等しい 3,600,000 ms

    • 1 日は に等しい 86,400,000 ms

    • 1 か月(または 30 日)は に等しい 2,592,000,000 ms

  • GeoJSON ポイントであるpivotはメートル単位で測定され、整数または浮動小数点数として指定する必要があります。

はい

score

オブジェクト

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

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

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

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

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

詳しくは、「 スコアリング動作 」を参照してください。

no

MongoDB Search score は、 MongoDB Search 結果の origin に対する近接性を測定します。score01 の間でスケーリングされ、1 は完全一致、0 は距離一致です。origin からのMongoDB Search の結果の距離が、pivot を使用して計算されたオリジンからの距離と等しい場合、スコアは 0.5 と等しくなります。

near演算子は、ドキュメントスコアを計算するために以下の距離減衰関数を使用します。

pivot
score = ------------------
pivot + distance

ここで、各期間は次のように定義されます。

係数
説明

pivot

fieldValueoriginの距離がそれに等しい場合に、スコアを0.5と等しくするための参照点として指定される値。 これは、 fieldValueoriginの距離が大きくなるにつれてスコアがどの程度早く低下するかを定義します。 fieldValueoriginの間の特定の距離の場合、 pivotが減少すると、スコアも減少します。

distance

fieldValueorigin の間の絶対距離。MongoDB Search は、次の式を使用してこの値を計算します。

abs(fieldValue - origin)

以下の条件に一致するもの。

  • fieldValue これは、ドキュメント内でクエリしているフィールドの値です。

  • origin は、付近を検索する番号、日付、または地理的な点です。

クエリでscoreオプションを使用してデフォルトのスコアを変更できます。 オプションの詳細については、 「 スコアの変更 」を参照してください。

The number and date examples use the movies collection in the sample_mflix database. The GeoJSON point example uses the listingsAndReviews collection in the sample_airbnb database.

If you load the sample data on your cluster, you can create the static indexes using the index definitions in the examples below or the dynamic index and run the example queries on your cluster.

Tip

If you've already loaded the sample dataset, refer to the MongoDB Search Quick Start tutorial to create an index definition and run MongoDB Search queries.

次の例では、near 演算子を使用して数値フィールドをクエリします。次のクエリは、movies コレクション内のすべてのフィールドに動的にインデックスを付ける runtimes という名前のインデックスを使用します。このクエリは、movies コレクション内で runtime フィールドの値が 279 に近いドキュメントを検索します。

次のクエリは、検索条件に一致するすべてのドキュメントを返します。出力を の結果に制限する$limit ステージと、次の操作を行う ステージが含まれています。7$project

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

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

scorepivotを使用して計算されます。

1db.movies.aggregate([
2 {
3 $search: {
4 "index": "runtimes",
5 "near": {
6 "path": "year",
7 "origin": 2000,
8 "pivot": 2
9 }
10 }
11 },
12 {
13 $limit: 7
14 },
15 {
16 $project: {
17 "_id": 0,
18 "title": 1,
19 "runtime": 1,
20 score: { $meta: "searchScore" }
21 }
22 }
23])
[
{ runtime: 279, title: 'The Kingdom', score: 1 },
{ runtime: 279, title: 'The Kingdom', score: 1 },
{ runtime: 279, title: 'The Jinx: The Life and Deaths of Robert Durst', score: 1 },
{ runtime: 281, title: 'Les Misèrables', score: 0.5 },
{ runtime: 277, title: 'Tokyo Trial', score: 0.5 },
{ runtime: 283, title: 'Scenes from a Marriage', score: 0.3333333432674408 },
{ runtime: 274, title: 'The Crimson Petal and the White', score: 0.2857142984867096 }
]

MongoDB Search の結果では、映画 The KingdomThe Jinx: The Life and Deaths of Robert Durst は、279runtimeフィールド値が完全一致であるため、1.0 のスコアを受け取ります。映画「Les Misèrables」と「Tokyo Trial」は、runtimeフィールド値が 279 から 2 単位であるため、スコアは 0.5 になります。

次のクエリは、検索条件に一致するメタデータの結果を返します。このクエリでは $searchMeta ステージを使用して、次のバケット(年)内で検索条件に一致する映画数を取得します。

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

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

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

  • 2010: 2010 の排他的上限

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "near": {
7 "path": "runtime",
8 "origin": 279,
9 "pivot": 2
10 }
11 },
12 "facets": {
13 "yearFacet": {
14 "type": "number",
15 "path": "year",
16 "boundaries": [2000, 2005, 2010, 2015 ]
17 }
18 }
19 }
20 }
21 }
22])
[
{
count: { lowerBound: Long('20910') },
facet: {
yearFacet: {
buckets: [
{ _id: 2000, count: Long('3058') },
{ _id: 2005, count: Long('4012') },
{ _id: 2010, count: Long('4669') }
]
}
}
}
]

次の例では、 near演算子を使用して日付フィールドをクエリします。

releaseddateという名前の次のインデックス定義は、 moviesコレクション内のreleasedフィールド値をインデックス化します。

1{
2 "mappings": {
3 "dynamic": false,
4 "fields": {
5 "released": {
6 "type": "date"
7 }
8 }
9 }
10}

次のクエリは、 13 、 1年 9 月 日頃に公開された映画を検索します。 出力を3の結果に制限する$limitステージと、次の操作を行う$projectステージが含まれています。

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

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

結果のscorepivotを使用して計算されます。

注意

pivot ここでは、 はミリ秒単位で測定され、 7,776,000,000 msは約 3 か月に等しくなります。

1db.movies.aggregate([
2 {
3 $search: {
4 "index": "releaseddate",
5 "near": {
6 "path": "released",
7 "origin": ISODate("1915-09-13T00:00:00.000+00:00"),
8 "pivot": 7776000000
9 }
10 }
11 },
12 {
13 $limit: 3
14 },
15 {
16 $project: {
17 "_id": 0,
18 "title": 1,
19 "released": 1,
20 score: { $meta: "searchScore" }
21 }
22 }
23])

上記のクエリは、次の検索結果を返します。

{ "title" : "Regeneration", "released" : ISODate("1915-09-13T00:00:00Z"), "score" : 1 }
{ "title" : "The Cheat", "released" : ISODate("1915-12-13T00:00:00Z"), "score" : 0.49723756313323975 }
{ "title" : "Hell's Hinges", "released" : ISODate("1916-03-05T00:00:00Z"), "score" : 0.34090909361839294 }

上記のMongoDB Search 結果では、映画「Regeneration」には 1 のスコアが与えられます。これは、1915-09-13releasedフィールド値が完全一致であるためです。1915-12-13 に公開された映画 The Cheat は、origin からの releasedフィールド値の距離が 1915-09-13 から約 7,776,000,000 ミリ秒であるため、約 0.5 のスコアを受け取ります。

次の例では、 near演算子を使用して、 sample_airbnb.listingsAndReviewsコレクション内の GeoJSON ポイント オブジェクトをクエリします。 address.locationproperty_type次のインデックス定義は、listingsAndReviews コレクション内の フィールドと フィールドにインデックスを作成します。

1{
2 "mappings": {
3 "fields": {
4 "address": {
5 "fields": {
6 "location": {
7 "type": "geo"
8 }
9 },
10 "type": "document"
11 },
12 "property_type": {
13 "type": "string"
14 }
15 }
16 }
17}

次の例では、 near演算子を使用して、 sample_airbnb.listingsAndReviewsコレクションのaddress.locationフィールドをクエリします。

The following query searches for properties in Portugal. It includes a $limit stage to limit the output to 3 results and a $project stage to:

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

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

結果のscorepivotを使用して計算されます。 ここでは、 pivotはメートル単位で測定され、1000 メートルは 1 キロメートルに等しいことに注意してください。

1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "near": {
5 "origin": {
6 "type": "Point",
7 "coordinates": [-8.61308, 41.1413]
8 },
9 "pivot": 1000,
10 "path": "address.location"
11 }
12 }
13 },
14 {
15 $limit: 3
16 },
17 {
18 $project: {
19 "_id": 0,
20 "name": 1,
21 "address": 1,
22 score: { $meta: "searchScore" }
23 }
24 }
25])

上記のクエリは、次の検索結果を返します。

1{
2 "name" : "Ribeira Charming Duplex",
3 "address" : {
4 "street" : "Porto, Porto, Portugal",
5 "suburb" : "",
6 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
7 "market" : "Porto",
8 "country" : "Portugal",
9 "country_code" : "PT",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ -8.61308, 41.1413 ],
13 "is_location_exact" : false
14 }
15 },
16 "score" : 1
17}
18{
19 "name" : "DB RIBEIRA - Grey Apartment",
20 "address" : {
21 "street" : "Porto, Porto, Portugal",
22 "suburb" : "",
23 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
24 "market" : "Porto",
25 "country" : "Portugal",
26 "country_code" : "PT",
27 "location" : {
28 "type" : "Point",
29 "coordinates" : [ -8.61294, 41.14126 ],
30 "is_location_exact" : true
31 }
32 },
33 "score" : 0.9876177310943604
34}
35{
36 "name" : "Ribeira 24 (4)",
37 "address" : {
38 "street" : "Porto, Porto, Portugal",
39 "suburb" : "",
40 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
41 "market" : "Porto",
42 "country" : "Portugal",
43 "country_code" : "PT",
44 "location" : {
45 "type" : "Point",
46 "coordinates" : [ -8.61318, 41.14107 ],
47 "is_location_exact" : false
48 }
49 },
50 "score" : 0.973789632320404
51}

結果は、指定された座標から離れたプロパティはスコアが低いことを示しています。

compound次の例では、property_type address.location演算子を使用して、sample_airbnb.listingsAndReviews コレクション内の フィールドと フィールドをクエリします。

The following query searches for apartments in Hong Kong near a specified GeoJSON point. The query uses must to specify the search condition, which must be met, and should to specify preference for location. It includes a $limit stage to limit the output to 3 results and a $project stage to:

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

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

scorepivotフィールドを使用して計算されます。 ここでは、 pivotはメートル単位で測定され、1000 メートルは 1 キロメートルに等しいことに注意してください。

1db.listingsAndReviews.aggregate([
2 {
3 $search: {
4 "compound": {
5 "must": {
6 "text": {
7 "query": "Apartment",
8 "path": "property_type"
9 }
10 },
11 "should": {
12 "near": {
13 "origin": {
14 "type": "Point",
15 "coordinates": [114.15027, 22.28158]
16 },
17 "pivot": 1000,
18 "path": "address.location"
19 }
20 }
21 }
22 }
23 },
24 {
25 $limit: 3
26 },
27 {
28 $project: {
29 "_id": 0,
30 "property_type": 1,
31 "address": 1,
32 score: { $meta: "searchScore" }
33 }
34 }
35])

上記のクエリは、次の検索結果を返します。

1{
2 "property_type" : "Apartment",
3 "address" : {
4 "street" : "Hong Kong, Hong Kong Island, Hong Kong",
5 "suburb" : "Central & Western District",
6 "government_area" : "Central & Western",
7 "market" : "Hong Kong",
8 "country" : "Hong Kong",
9 "country_code" : "HK",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ 114.15027, 22.28158 ],
13 "is_location_exact" : true
14 }
15 },
16 "score" : 1.177286982536316
17}
18{
19 "property_type" : "Apartment",
20 "address" : {
21 "street" : "Hong Kong, Hong Kong Island, Hong Kong",
22 "suburb" : "Central & Western District",
23 "government_area" : "Central & Western",
24 "market" : "Hong Kong",
25 "country" : "Hong Kong",
26 "country_code" : "HK",
27 "location" : {
28 "type" : "Point",
29 "coordinates" : [ 114.15082, 22.28161 ],
30 "is_location_exact" : true
31 }
32 },
33 "score" : 1.1236450672149658
34}
35{
36 "property_type" : "Apartment",
37 "address" : {
38 "street" : "Hong Kong,
39 Hong Kong Island, Hong Kong",
40 "suburb" : "Mid-Levels",
41 "government_area" : "Central & Western",
42 "market" : "Hong Kong",
43 "country" : "Hong Kong",
44 "country_code" : "HK",
45 "location" : {
46 "type" : "Point",
47 "coordinates" : [ 114.15007, 22.28215 ],
48 "is_location_exact" : true
49 }
50 },
51 "score" : 1.114811897277832
52}