

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

# \$1meta
<a name="meta-aggregation"></a>

Der `$meta` Aggregationsoperator greift auf Metadaten zu, die Dokumenten in einer Aggregationspipeline zugeordnet sind. Er wird häufig verwendet, um Ergebnisse der Textsuche abzurufen und Ergebnisse nach Relevanz zu sortieren.

**Parameter**
+ `textScore`: Ruft das Ergebnis der Textsuche ab, das die Relevanz des Dokuments für die Suchabfrage angibt.

## Beispiel (MongoDB Shell)
<a name="meta-aggregation-examples"></a>

Das folgende Beispiel zeigt die Verwendung des `$meta` Operators in einer Aggregationspipeline zum Abrufen und Sortieren nach Textsuchergebnissen.

**Erstellen Sie Beispieldokumente**

```
db.articles.createIndex({ content: "text" });

db.articles.insertMany([
  { _id: 1, title: "Python Programming", content: "Python is a versatile programming language used for web development." },
  { _id: 2, title: "Python Guide", content: "Learn Python programming with Python tutorials and Python examples." },
  { _id: 3, title: "Java Basics", content: "Java is another popular programming language." }
]);
```

**Beispiel für eine Abfrage**

```
db.articles.aggregate([
  { $match: { $text: { $search: "Python" } } },
  { $addFields: { score: { $meta: "textScore" } } },
  { $sort: { score: -1 } }
]);
```

**Ausgabe**

```
[
  {
    _id: 2,
    title: 'Python Guide',
    content: 'Learn Python programming with Python tutorials and Python examples.',
    score: 1.5
  },
  {
    _id: 1,
    title: 'Python Programming',
    content: 'Python is a versatile programming language used for web development.',
    score: 0.75
  }
]
```

## Codebeispiele
<a name="meta-aggregation-code"></a>

Um ein Codebeispiel für die Verwendung des `$meta` Aggregationsoperators anzuzeigen, wählen Sie die Registerkarte für die Sprache, die Sie verwenden möchten:

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function example() {
  const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
  const db = client.db('test');
  const collection = db.collection('articles');

  const result = await collection.aggregate([
    { $match: { $text: { $search: "Python" } } },
    { $addFields: { score: { $meta: "textScore" } } },
    { $sort: { score: -1 } }
  ]).toArray();

  console.log(result);
  await client.close();
}

example();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def example():
    client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
    db = client['test']
    collection = db['articles']

    result = list(collection.aggregate([
        { '$match': { '$text': { '$search': 'Python' } } },
        { '$addFields': { 'score': { '$meta': 'textScore' } } },
        { '$sort': { 'score': -1 } }
    ]))

    print(result)
    client.close()

example()
```

------