English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
부터2.4버전 시작부터 MongoDB는 문자열 내용을 검색하기 위해 텍스트 인덱스를 지원하기 시작했습니다. Text Search(텍스트 검색)은 단어 트리밍 기술을 사용하여 문자열 필드에서 단어 트리밍 중지어(예: a, an, The 등)를 제거하여 지정된 단어를 검색합니다. 현재 MongoDB는 약15언어로 사용할 수 있습니다.
처음에는 "Text Search(텍스트 검색)"은 실험적인 기능이었지만,2.6판에서는 기본적으로 이 구성을 활성화했습니다.
다음과 같이 고려해야 합니다posts
콜렉션에 포함된 문서, 게시물 텍스트 및 태그-
> db.posts.insert({ "post_text": "enjoy the mongodb articles on w3codebox", "tags": ["mongodb", "w3codebox"] } { "post_text": "writing tutorials on mongodb", "tags": ["mongodb", "tutorial"] ) WriteResult({ "nInserted": 1 )
우리는 post_text 필드에 텍스트 인덱스를 생성하여 게시물의 텍스트에서 검색할 수 있도록 합니다-
>db.posts.createIndex({post_text:"text"}) { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 }
이제 post_text 필드에 텍스트 인덱스를 생성했습니다. 우리는 게시물의 텍스트에서 검색w3codebox
텍스트에 포함된 단어의 모든 게시물.
> db.posts.find({$text:{$search:"w3codebox"}}).pretty(){ "_id": ObjectId("5dd7ce28f1dd4583e7103fe0"), "post_text": "enjoy the mongodb articles on w3codebox", "tags": [ "mongodb", "w3codebox" ] }
위 명령어는 다음 결과 문서를 반환합니다. 이 결과 문서w3codebox
의 게시 텍스트에 포함된 단어:
{ "_id": ObjectId("53493d14d852429c10000002", "post_text": "enjoy the mongodb articles on w3codebox", "tags": ["mongodb", "w3codebox"]}
존재하는 텍스트 인덱스를 삭제하기 전에 먼저 다음 쿼리를 사용하여 인덱스 이름을 찾아야 합니다-
>db.posts.getIndexes()[ { "v" : 2, "key" : { "_id": 1 }, "name": "_id_", "ns" : "mydb.posts" }, { "v" : 2, "key" : { "fts" : "text", "ftsx" : 1 }, "name" : "post_text_text", "ns" : "mydb.posts", "weights" : { "post_text" : 1 }, "default_language" : "english", "language_override" : "language", "textIndexVersion" : 3 } ] >
위 쿼리에서 인덱스 이름을 가져온 후, 다음 명령을 실행합니다. 여기서는post_text_text
은 인덱스 이름입니다.
>db.posts.dropIndex("post_text_text") { "nIndexesWas" : 2, "ok" : 1 }