다음을 통해 공유


$meta

$meta 프로젝션 연산자는 쿼리 결과에 메타데이터를 포함하는 데 사용됩니다. 출력 문서에 텍스트 검색 점수 또는 기타 계산 값과 같은 메타데이터를 포함하는 데 유용합니다.

문법

프로젝션 연산자를 사용하는 $meta 구문은 다음과 같습니다.

db.collection.find({
    $text: {
        $search: < string >
    }
}, {
    field: {
        $meta: < metaDataKeyword >
    }
})

매개 변수

매개 변수 Description
field 메타데이터가 포함되는 출력 문서의 필드 이름입니다.
metaDataKeyword 텍스트 검색 점수와 같은 textScore 일반적인 키워드를 포함할 메타데이터의 형식입니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
  "_id": "34f462fe-5085-4a77-a3de-53f4117466bd",
  "name": "Wide World Importers",
  "location": {
    "lat": -63.5435,
    "lon": 77.7226
  },
  "staff": {
    "totalStaff": {
      "fullTime": 16,
      "partTime": 16
    }
  },
  "sales": {
    "totalSales": 41481,
    "salesByCategory": [
      {
        "categoryName": "Holiday Tableware",
        "totalSales": 41481
      }
    ]
  },
  "promotionEvents": [
    {
      "eventName": "Crazy Deal Days",
      "promotionalDates": {
        "startDate": {
          "Year": 2023,
          "Month": 11,
          "Day": 13
        },
        "endDate": {
          "Year": 2023,
          "Month": 11,
          "Day": 22
        }
      },
      "discounts": [
        {
          "categoryName": "Gift Boxes",
          "discountPercentage": 9
        },
        {
          "categoryName": "Holiday Tableware",
          "discountPercentage": 24
        }
      ]
    },
    {
      "eventName": "Incredible Savings Showcase",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 5,
          "Day": 11
        },
        "endDate": {
          "Year": 2024,
          "Month": 5,
          "Day": 20
        }
      },
      "discounts": [
        {
          "categoryName": "Ribbons",
          "discountPercentage": 15
        },
        {
          "categoryName": "Gift Bags",
          "discountPercentage": 25
        }
      ]
    }
  ],
  "tag": [
    "#ShopLocal",
    "#FashionStore",
    "#SeasonalSale",
    "#FreeShipping",
    "#MembershipDeals"
  ]
}

예제 1: 텍스트 검색 점수 포함

텍스트 검색 쿼리의 결과에 텍스트 검색 점수를 포함하려면

db.stores.createIndex({ "name": "text"});

db.stores.find(
    { $text: { $search: "Equipment Furniture Finds" } },
    { _id: 1, name: 1, score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } }).limit(2)

이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.

[
  {
    _id: 'cf1448e9-5493-49b5-95da-ab8a105b5240',
    name: 'Tailwind Traders | Camera Market - Wolfmouth',
    score: 4
  },
  {
    _id: '4fd389af-4693-4c02-93cf-0d80ae8ace07',
    name: 'Wide World Importers | Camera Collection - South Cordelia',
    score: 4
  }
]

Limitation

  • 인덱스가 사용되지 않으면 { $meta: "indexKey" }는 아무 것도 반환하지 않습니다.