Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

Element Query Operators

Element Query Operators are MongoDB Operators that can be used to find documents matching
the specified fields or types.
There are two types of Element Operators provided by MongoDB.

Name Description
$exists we use the $exists operator to find the
documents that have the specified field.
$type We use the $type operator to find the documents
that have the specified data types.

Syntax:
•$exists - db.collection_name.find( { <field_name>: { $exists: <boolean_value> } } )
•$type - db.collection_name.find( { <field_name>: { $type: < BSON_Type> } } )
What is an index?
 A database index is a special data structure that provides faster access to data and
helps create highly performant applications.
 An index usually consists of two columns: the search key and the data pointer.
 The key stores the value you want to search for and the pointer points to the
block where the data resides.
 Let’s say you have an index on the ‘score’ field (column) of a collection (table)
named 'exam’.
 When you want to access the scores that are < 40, the index will scan through
blocks rather than individual documents (rows).
Why indexing is important
 Without an index, the database engine needs to go through each record to see if
there is a match.
 A database index is a way to organize information so that the database engine
can quickly find the relevant results.
 You can save significant time on grouping, sorting, and retrieving data.
Databases allow you to add multiple indexes, further increasing the query
efficiency.
How do you use a database index?
 Think of a database index to be similar to that of a book index (usually at the end
of the book).
 Each keyword is mentioned in alphabetical order along with the page numbers
where the word is used, enabling users to search for the word quickly.
 Database indexes work in the same way. Suppose you have 1000 keyword names
stored in the database. If they are sorted by name (a->z or z->a), it is easier for
the query to look up the last keyword, middle keyword, or a specific keyword,
based on the sort.
db.std.createIndex({population : 1})

Index Types

Single Field Index


Single field indexes collect and sort data from a single field in each document in a collection.

Compound Index
Compound indexes collect and sort data from two or more fields in each document in a collection. Data is grouped by the first
field in the index and then by each subsequent field.

Multikey Index
Multikey indexes collect and sort data stored in arrays.
You do not need to explicitly specify the multikey type. When you create an index on a field that contains an array value,
MongoDB automatically sets the index to be a multikey index.

Text Index
Text indexes support text search queries on fields containing string content.
Text indexes support text search queries on fields containing string content. Text
indexes improve performance when searching for specific words or phrases within
string content.
A collection can only have one text index, but that index can cover multiple fields.
To create a text index, use the following prototype:

db.<collection>.createIndex(
{
<field1>: "text",
<field2>: "text",
...
}
)
db.studentsposts.createIndex({title: "text"})

Now we will see how to search using Text Index:


db.studentsposts.find({$text:{$search: "mongodb"}}).pretty()

You might also like