$meta - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$meta

$meta 연산자는 현재 쿼리 실행과 연결된 메타데이터에 액세스하는 데 사용됩니다. 이 연산자는 주로 텍스트 검색 작업에 사용되며, 메타데이터는 일치하는 문서의 관련성에 대한 정보를 제공할 수 있습니다.

파라미터

  • textScore: 문서의 텍스트 검색 점수를 검색합니다. 이 점수는 텍스트 검색 쿼리와 문서의 관련성을 나타냅니다.

예제(MongoDB 쉘)

다음 예제에서는 $meta 연산자를 사용하여 텍스트 검색 쿼리와 일치하는 문서의 텍스트 검색 점수를 검색하는 방법을 보여줍니다.

샘플 문서 생성

db.documents.insertMany([ { _id: 1, title: "Coffee Basics", content: "Coffee is a popular beverage made from roasted coffee beans." }, { _id: 2, title: "Coffee Culture", content: "Coffee coffee coffee - the ultimate guide to coffee brewing and coffee preparation." }, { _id: 3, title: "Tea vs Coffee", content: "Many people prefer tea over coffee for its health benefits." } ]);

텍스트 인덱스 생성

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

쿼리 예제

db.documents.find( { $text: { $search: "coffee" } }, { _id: 0, title: 1, content: 1, score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } });

출력

[ { title: 'Coffee Culture', content: 'Coffee coffee coffee - the ultimate guide to coffee brewing and coffee preparation.', score: 0.8897688388824463 }, { title: 'Coffee Basics', content: 'Coffee is a popular beverage made from roasted coffee beans.', score: 0.75990891456604 }, { title: 'Tea vs Coffee', content: 'Many people prefer tea over coffee for its health benefits.', score: 0.6079270839691162 } ]

코드 예제

$meta 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function findWithTextScore() { 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('documents'); const result = await collection.find( { $text: { $search: "coffee" } }, { projection: { _id: 0, title: 1, content: 1, score: { $meta: "textScore" } } } ).sort({ score: { $meta: "textScore" } }).toArray(); console.log(result); client.close(); } findWithTextScore();
Python
from pymongo import MongoClient 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['documents'] for doc in collection.find( {'$text': {'$search': 'coffee'}}, {'_id': 0, 'title': 1, 'content': 1, 'score': {'$meta': 'textScore'}} ).sort([('score', {'$meta': 'textScore'})]): print(doc) client.close()