Document-oriented database, MongoDB is a type of NoSQL database. Different types of operators are available in MongoDB for usage when interacting with the database.
Projection operators perform the different operations on the array to project elements. The projection operators available in MongoDB are listed below:
$
$elemMatch
$meta
$slice
We'll use the following database to perform different projection operators where the database name is educative
, and it has a collection named courses
.
use educative //selecting our databasedb.courses.find({}) //showing all the documents of "courses"[{ _id: 10, course_name: 'python', hours: [10,14,20] },{ _id: 11, course_name: 'C++', hours: [10,15] },{ _id: 12, course_name: 'java', hours: [10,11,12] }]
Note: To learn about array operators in MongoDB, click here.
$
operatorThe $
operator is used to match the first element from an array that is specified in the given query condition. Only a single $
operator can be used in one query.
The syntax of $
operator is:
db.collection.find({ <array.field>: <condition> ...},{ "<array>.$": 1 })
Here's an example of $
operator:
//querydb.courses.find( {hours: { $gte: 12}}, {"hours.$": 1})//output[{ _id: 10, hours: [ 14 ] },{ _id: 11, hours: [ 15 ] },{ _id: 12, hours: [ 12 ] }]
The above query returns only the first value of hours
from each document that is greater or equal to 12
.
$elemMatch
operatorThe $elemMatch
operator matches documents with an array field with at least one element that satisfies all the specified query criteria.
Here's the syntax of $elemMatch
operator:
{<field>:{$elemMatch:{<query1>, <query2>, ...}}}
An example of the $elemMatch
operator is given below:
//querydb.courses.find({ "hours": { $elemMatch: {$gte: 11, $lt: 15}}}).pretty()//output[{ _id: 10, course_name: 'python', hours: [ 10, 14, 20 ] },{ _id: 12, course_name: 'java', hours: [ 10, 11, 12 ] }]
The $elemMatch
query returns only those hours
values that are greater than or equal to 11
and less than 15
.
$meta
operatorThe $meta
operator is used to see the metadata or description of the document while searching text.
The syntax of $meta
operator is:
{ $meta: <metaDataKeyword> }
The $meta
operator can be used with keyword textScore
that returns the score of associated $text
query for each matching document.
Note: We can use
$meta
within asort()
function but thetextScore
metadata will be sorted in descending order.
Here's an example of $meta
operator:
Step 1: Create an index for the field course_name
:
db.courses.createIndex({"course_name": "text"})
Step 2: Run the $meta
query to search text
:
//querydb.courses.find({$text : {$search: "python"}},{score: {$meta: "textScore"},_id: 0})//output[ { course_name: ‘python’, hours: [ 10, 14, 20 ], score: 1.1 } ]
The above query returns the document that contains the text python
and textScore
associated with this text
.
Note: The
_id: 0
field is used to omit theobject_id
of each document for a more straightforward output.
$slice
operatorThe $slice
operator is used to limit or skip the number of elements in an array that are specified in the query.
The syntax of $slice
operator is:
db.collection.find(<query>,{ <arrayField>: { $slice: <number> } });//ordb.collection.find(<query>,{ <arrayField>: { $slice: [ <number to skip>, <number to return> ] } });
Here's an example of $slice
operator:
//querydb.courses.find( { }, { _id: 0, "hours": { $slice: 1 } } )//output[{ course_name: 'python', hours: [ 10 ] },{ course_name: 'C++', hours: [ 10 ] },{ course_name: 'java', hours: [ 10 ] }]
The above query returned only the first values of the hours
as we have used 1
in the query.
You can run all the aforementioned MongoDB queries in the terminal below:
Free Resources