|
| 1 | +--- |
| 2 | +title: $meta |
| 3 | +description: The $meta operator returns a calculated metadata column with returned dataset. |
| 4 | +type: operators |
| 5 | +category: projection |
| 6 | +--- |
| 7 | + |
| 8 | +# $meta |
| 9 | + |
| 10 | +The `$meta` projection operator is used to include metadata in the results of a query. It's useful for including metadata such as text search scores or other computed values in the output documents. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +The syntax for using the `$meta` projection operator is as follows: |
| 15 | + |
| 16 | +```javascript |
| 17 | +db.collection.find({ |
| 18 | + $text: { |
| 19 | + $search: < string > |
| 20 | + } |
| 21 | +}, { |
| 22 | + field: { |
| 23 | + $meta: < metaDataKeyword > |
| 24 | + } |
| 25 | +}) |
| 26 | +``` |
| 27 | + |
| 28 | +## Parameters |
| 29 | + |
| 30 | +| Parameter | Description | |
| 31 | +| --- | --- | |
| 32 | +| **`field`** | The name of the field in the output documents where the metadata gets included. | |
| 33 | +| **`metaDataKeyword`** | The type of metadata to include common keywords like `textScore` for text search scores. | |
| 34 | + |
| 35 | +## Examples |
| 36 | + |
| 37 | +Consider this sample document from the stores collection. |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "_id": "34f462fe-5085-4a77-a3de-53f4117466bd", |
| 42 | + "name": "Wide World Importers", |
| 43 | + "location": { |
| 44 | + "lat": -63.5435, |
| 45 | + "lon": 77.7226 |
| 46 | + }, |
| 47 | + "staff": { |
| 48 | + "totalStaff": { |
| 49 | + "fullTime": 16, |
| 50 | + "partTime": 16 |
| 51 | + } |
| 52 | + }, |
| 53 | + "sales": { |
| 54 | + "totalSales": 41481, |
| 55 | + "salesByCategory": [ |
| 56 | + { |
| 57 | + "categoryName": "Holiday Tableware", |
| 58 | + "totalSales": 41481 |
| 59 | + } |
| 60 | + ] |
| 61 | + }, |
| 62 | + "promotionEvents": [ |
| 63 | + { |
| 64 | + "eventName": "Crazy Deal Days", |
| 65 | + "promotionalDates": { |
| 66 | + "startDate": { |
| 67 | + "Year": 2023, |
| 68 | + "Month": 11, |
| 69 | + "Day": 13 |
| 70 | + }, |
| 71 | + "endDate": { |
| 72 | + "Year": 2023, |
| 73 | + "Month": 11, |
| 74 | + "Day": 22 |
| 75 | + } |
| 76 | + }, |
| 77 | + "discounts": [ |
| 78 | + { |
| 79 | + "categoryName": "Gift Boxes", |
| 80 | + "discountPercentage": 9 |
| 81 | + }, |
| 82 | + { |
| 83 | + "categoryName": "Holiday Tableware", |
| 84 | + "discountPercentage": 24 |
| 85 | + } |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "eventName": "Incredible Savings Showcase", |
| 90 | + "promotionalDates": { |
| 91 | + "startDate": { |
| 92 | + "Year": 2024, |
| 93 | + "Month": 5, |
| 94 | + "Day": 11 |
| 95 | + }, |
| 96 | + "endDate": { |
| 97 | + "Year": 2024, |
| 98 | + "Month": 5, |
| 99 | + "Day": 20 |
| 100 | + } |
| 101 | + }, |
| 102 | + "discounts": [ |
| 103 | + { |
| 104 | + "categoryName": "Ribbons", |
| 105 | + "discountPercentage": 15 |
| 106 | + }, |
| 107 | + { |
| 108 | + "categoryName": "Gift Bags", |
| 109 | + "discountPercentage": 25 |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "tag": [ |
| 115 | + "#ShopLocal", |
| 116 | + "#FashionStore", |
| 117 | + "#SeasonalSale", |
| 118 | + "#FreeShipping", |
| 119 | + "#MembershipDeals" |
| 120 | + ] |
| 121 | +} |
| 122 | + |
| 123 | +``` |
| 124 | + |
| 125 | +### Example 1: Including text search scores |
| 126 | + |
| 127 | +To include the text search score in the results of a text search query. |
| 128 | + |
| 129 | +```javascript |
| 130 | +db.stores.createIndex({ "name": "text"}); |
| 131 | + |
| 132 | +db.stores.find( |
| 133 | + { $text: { $search: "Equipment Furniture Finds" } }, |
| 134 | + { _id: 1, name: 1, score: { $meta: "textScore" } } |
| 135 | +).sort({ score: { $meta: "textScore" } }).limit(2) |
| 136 | + |
| 137 | +``` |
| 138 | + |
| 139 | +The first two results returned by this query are: |
| 140 | + |
| 141 | +```json |
| 142 | +[ |
| 143 | + { |
| 144 | + _id: 'cf1448e9-5493-49b5-95da-ab8a105b5240', |
| 145 | + name: 'Tailwind Traders | Camera Market - Wolfmouth', |
| 146 | + score: 4 |
| 147 | + }, |
| 148 | + { |
| 149 | + _id: '4fd389af-4693-4c02-93cf-0d80ae8ace07', |
| 150 | + name: 'Wide World Importers | Camera Collection - South Cordelia', |
| 151 | + score: 4 |
| 152 | + } |
| 153 | +] |
| 154 | +``` |
| 155 | + |
| 156 | +## Limitation |
| 157 | + |
| 158 | +- If no index is used, the { $meta: "indexKey" } doesn't return anything. |
0 commit comments