$search - Amazon DocumentDB

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

$search

버전 5.0에서 새로 추가되었습니다.

Amazon DocumentDB의 $search 연산자는 텍스트 검색 기능을 제공하는 데 사용됩니다.

파라미터

없음

예제(MongoDB 쉘)

다음 예제에서는 $search 연산자를 사용하여 텍스트 검색 쿼리를 수행하는 방법을 보여줍니다.

샘플 문서 생성

db.textcollection.createIndex({"description": "text"}); db.textcollection.insertMany([ { _id: 1, name: "John Doe", description: "This is a sample document about John Doe." }, { _id: 2, name: "Jane Smith", description: "This is a sample document about Jane Smith." }, { _id: 3, name: "Bob Johnson", description: "This is a sample document about Bob Johnson." }, { _id: 4, name: "Jon Jeffries", description: "This is a sample document about Jon Jeffries." } ]);

쿼리 예제

db.textcollection.find( { $text: { $search: "John" } } );

출력

[ { _id: 1, name: 'John Doe', description: 'This is a sample document about John Doe.' } ]

코드 예제

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

Node.js
const { MongoClient } = require('mongodb'); async function findWithText() { 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('textcollection'); const result = await collection.find( { $text: { $search: "John" } } ).sort({ score: { $meta: "textScore" } }).toArray(); console.log(result); client.close(); } findWithText();
Python
from pymongo import MongoClient def find_with_text(): 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['textcollection'] result = list(collection.find( { '$text': { '$search': 'John' } } )) print(result) client.close() find_with_text()