News
Seven days of Black Friday madness — great deal from Serverspace!
Serverspace Black Friday
RF
February 14, 2024
Updated February 14, 2024

How to Text Search in MongoDB: sampling and filtering

Linux MongoDB

This tutorial focused on attaining proficiency in text search within MongoDB, featuring advanced methodologies such as sampling and filtering. In the dynamic landscape of MongoDB databases, adept text searches play a pivotal role in extracting pertinent information. Uncover essential strategies to refine search accuracy and optimize performance within your MongoDB framework. Whether you are a novice or a seasoned MongoDB user, this guide is designed to empower you, enabling a heightened mastery of text search capabilities. Join us as we embark on a comprehensive exploration to unlock the unparalleled potential of text searching in MongoDB.

Before we start consider various method to filtering and sampling text, we need to deploy server, that you can do on Serverspace cloud servers or use VM. Search in the left menu cloud platform and press on the button Create Server.

Create machine
Screenshot №1 — Create machine

Then you can configure machine with various parameters: CPU, RAM, disk space and choose needed size accord with your requirements and budget! After that you can deploy MongoDB server on them or continue make configuration on your own server!

Filtering and Sampling

In the MongoDB we have function for conditional search or sampling data and filtering, that's db.name_of_collection.find(). If you can notice language of requests is quite similar with programming language with their class, entity and method. Also as in the SQL language in the custom from MongoDB we can translate our query, for example. If we type:

db.ff_collection.find()
Find by default
Screenshot №2 — Find by default

That means look for all files in the default database in the ff_collection and obviously display that. Take in mind! Before usage any request you need to switch database with command and replace database_name with yours:

use database_name

Also, if you need add text data for your db and take test examples with them:

db.ff_collection.insertOne({"surname":"Garepton","occupation":"Actor","education_degree":"Finished"}
db.ff_collection.insertOne({"surname":"Gorphy","occupation":"Musician","education_degree":"Finished"})
db.ff_collection.insertOne({"surname":"Freeman","occupation":"Engineer","education_degree":"Unfinished"})
Examples
Screenshot №3 — Examples

Look at the different options with similar function, we can find one, find one and delete or replace, obviously update data:

Similar function
Screenshot №4 — Similar function

For filter data we need use find or findOne function accord to our requirements and choose field with searching value:

db.ff_collection.find({"surname":"Freeman"})
Search by one criteria
Screenshot №5 — Search by one criteria

For search only one document type follow command:

db.ff_collection.findOne({"surname":"Gorg"})
findOne function
Screenshot №6 — findOne function

As you can see we can indicate field with value for search and all document will be displayed. That function accordance to requirements to fast search data in the different document and also search in the complex object.

How to filter two fields in MongoDB?

For filter or sampling data with two parameters we need include second field with value in the curly braces, like that:

db.ff_collection.find({"education_degree":"Finished", "surname":"Gorphy"})
Filter by two value
Screenshot №7 — Filter by two value

In the first curly braces we indicate sign for filter data or we can name it - condition for search data. And list them separated by commas.

How to filter array values in MongoDB?

For filter documents by array values we need indicate name of array with value or values that depend on your requirements to the result. For search data in the array we add test data by command:

db.ff_collection.insertOne({"surname":"Gorg",occupation:["Dancer", "Actor"]})

If you have yours data, then skip that step. We can search by one value in the array using command below:

db.ff_collection.find({occupation:"Dancer"})
Search document by one arrays value
Screenshot №8 — Search document by one arrays value

If we have task for search strict sequences of values, than we indicate accordance array in the request:

db.ff_collection.find({occupation:["Dancer","Actor"]})
Search document by arrays values
Screenshot №9 — Search document by arrays values

But, if that again doesn't meet your request, then what to do? We can use another one filter, that help to indicate order or number of search value in the array, command below:

db.ff_collection.find({"occupation.0" :"Dancer"})
Search document by arrays number and values
Screenshot №10 — Search document by arrays number and values

Also that command we can translate as search in the default db and ff_collection, array with name occupation and value Dancer in the first cell of array. You can notice, usually, arrays name doesn't include in the curl braces, but that case exclude of common rules, due to reason of complex filter.

How to filter empty values in MongoDB?

For next filter, imagine situation when we need to search incorrect fill document and we need to search them, by missed filed. In that case we will use null:

db.ff_collection.find({education_degree:null})
Search by null value
Screenshot №11 — Search by null value

In cases when we need to search in the many of another file the one document with null value and another parameter, for example surname:Gorg, command below:

db.ff_collection.find({education_degree:null, "surname":"Gorg"})
Search by null and another values
Screenshot №12 — Search by null and another values

Next point, will help optimize our query and represent how to display indicated field instead all of finded document:

db.ff_collection.find({occupation:"Engineer"},{surname:1})

That query display surname and _id for finded document with condition occupation. If you want to hide surname and displayed other fileds change value to surname:0. Also for hide _id use command below:

db.ff_collection.find({occupation:"Engineer"},{surname:1,_id:0})
Search by values and filter them
Screenshot №13 — Search by values and filter them

In conclusion, this tutorial has provided a comprehensive exploration of text searching within MongoDB, with a focus on advanced methodologies such as sampling and filtering. The dynamic nature of MongoDB databases demands adept text searches to extract relevant information, and this guide aims to empower both novice and seasoned MongoDB users.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 4
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.