When you’re storing string data in MongoDB, there may be times when you need to perform a full-text search on this data. This functionality was first introduced in MongoDB with version 2.4. At that point, the feature was considered experimental; today, full-text search is a key component of MongoDB. In this tutorial, we’ll provide an overview of the full-text search feature and show you how to perform this type of search in MongoDB. Show PrerequisiteBefore attempting the examples in this tutorial, make sure that MongoDB is installed and configured on your machine. Full-text Search BasicsA good way to understand the concept of full-text search is to think about a typical Google search. When we use Google, we find content by providing a series of text, strings, phrases or keywords; in return, a number of results will be returned. In MongoDB, full-text search allows you to perform complex queries that are similar to those you’d perform using a search engine. You can search for phrases and stemmed variations on a word, and it’s also possible to exclude certain “negated” terms from your results. Here are a couple of common scenarios where full-text search plays a key role:
Create Sample DatasetNow that we’ve gone over the basics of how full-text search works, let’s create a sample dataset that we’ll use for the examples in this tutorial. The first step is to connect to your MongoDB server and perform the following commands: > use persondb MongoDB will return a response when the insert operation is complete. The result should look something like this: { To verify if the insert operation was a success, use the following command: “
The output should look something like this: 1 { Create Text Index in MongoDBIn this section, we’ll create a text index. This functionality is provided by MongoDB to support text search on strings within MongoDB collections or documents. We can use the following command to create a text index: The output will look like this: { Full-Text Search ExampleSo far, we’ve created our sample dataset and created a text index that will help support our full-text search. In this section, we’ll go ahead and perform a basic full-text search. Let’s try using the following query to find all person documents where the associated quote contains the word “war”: db.person.find( { $text: { $search: "war" } } ).pretty(); The results should look something like this: { ConclusionIf you’re managing string content in MongoDB, you’ll want to harness the power of full-text search to make your queries more efficient and accurate. In this tutorial, we showed you how to perform a simple full-text search on a sample dataset. With these instructions and examples, you’ll be able to search text fields within your own MongoDB documents. MongoDB menggunakan bahasa apa?MongoDB sendiri ditulis dengan bahasa C++ dan telah tersedia untuk berbagai jenis bahasa pemrograman. Fitur utama dari mongoDB antara lain: model document-oriented storage.
Kenapa harus menggunakan MongoDB?MongoDB mampu menampung lebih banyak data kompleks karena menggunakan schema table yang dinamis (dynamic schema). Dengan skema data tersebut, database ini bisa menyimpan data yang lebih bervariasi, mulai dari data terstruktur hingga tidak terstruktur.
Mengapa MongoDB disebut database berorientasi dokumen?Berorientasi pada dokumen-Karena MongoDB adalah database tipe NoSQL, alih-alih memiliki data dalam format tipe relasional, MongoDB menyimpan data dalam dokumen. Ini membuat MongoDB sangat fleksibel dan mudah beradaptasi dengan situasi dan persyaratan dunia bisnis nyata.
Apakah MongoDB termasuk database?MongoDB adalah database yang berorientasi pada dokumen. Data apa pun yang disimpan di dalam MongoDB itu berbentuk dokumen, bukan tabel. Dalam dokumen-dokumen tersebut, datamu disimpan dalam field, bukan baris atau kolom. Hal ini juga yang mendukung fleksibilitas MongoDB.
|