{"id":346,"date":"2024-08-11T20:46:35","date_gmt":"2024-08-11T19:46:35","guid":{"rendered":"https:\/\/codebounce.debojyotichatterjee.com\/?p=346"},"modified":"2024-11-17T16:11:16","modified_gmt":"2024-11-17T16:11:16","slug":"mongodb-query-and-projection-operators","status":"publish","type":"post","link":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/2024\/08\/11\/mongodb-query-and-projection-operators\/","title":{"rendered":"MongoDB Query and Projection Operators"},"content":{"rendered":"\n<p><strong>MongoDB<\/strong> is one of the most commonly used <strong>NoSQL databases.<\/strong> Query operators and projection operators in MongoDB offer fine control over which documents are retrieved and what format to apply to the resulting data set.<\/p>\n\n\n\n<p>We will shortly cover the different types of query and projection operators that are available within MongoDB. From filtering documents through to combining multiple conditions.<\/p>\n\n\n\n<p>&nbsp;This reference will essentially describe all these operators through clear examples thus serving as a<br>quick ref or cheat-sheet.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>For testing purposes you can use:<br>Dummy Data Set:\u00a0<a href=\"https:\/\/gist.github.com\/debojyotichatterjee9\/3b134f18700aef047a7b50fa56713657\" target=\"_blank\" rel=\"noopener sponsored ugc\" title=\"Dummy Data\">https:\/\/gist.github.com\/debojyotichatterjee9\/3b134f18700aef047a7b50fa56713657<\/a><br>Mongo Playground:\u00a0<a href=\"https:\/\/mongoplayground.net\/p\/SAIN3Opb3ci\" target=\"_blank\" rel=\"noopener sponsored ugc\" title=\"Mongo DB Playground\">https:\/\/mongoplayground.net\/p\/SAIN3Opb3ci<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Query Operators<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Comparison Operators<\/strong><\/h4>\n\n\n\n<p>Comparison operators in MongoDB are used to compare field values against a specified value. They are essential for filtering documents based on specific criteria.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$eq<\/code>&nbsp;(Equal)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is equal to the specified value.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    $eq: 73\n  }\n})\n\/\/ Finds users who are exactly 73 years old.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$gt<\/code>&nbsp;(Greater Than)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is greater than the specified value.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    $gt: 21\n  }\n})\n\/\/ Finds users who are older than 21.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$gte<\/code>&nbsp;(Greater Than or Equal)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is greater than or equal to the specified value.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    $gte: 21\n  }\n})\n\/\/ Finds users who are 21 years old or older.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$in<\/code> (In)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is in the specified array.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  username: {\n    $in: &#91;\n      \"rahul77\",\n      \"scotty76\"\n    ]\n  }\n})\n\/\/ Finds users with a username of either \"rahul77\" or \"scotty76\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$lt<\/code>&nbsp;(Less Than)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is less than the specified value.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    $lt: 30\n  }\n})\n\/\/ Finds users who are younger than 30.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$lte<\/code>&nbsp;(Less Than or Equal)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is less than or equal to the specified value.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    $lt: 30\n  }\n})\n\/\/ Finds users who are 30 years old or younger.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$ne<\/code>&nbsp;(Not Equal)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is not equal to the specified value.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  username: {\n    $ne: \"danny.pfeffer\"\n  }\n})\n\/\/ Finds users username status is not \"danny.pfeffer\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$nin<\/code>&nbsp;(Not In)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the field value is not in the specified array.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  username: {\n    $nin: &#91;\n      \"danny.pfeffer\",\n      \"rahul77\",\n      \"scotty76\"\n    ]\n  }\n})\/\/ Finds users whose username is neither \"danny.pfeffer\",\"rahul77\",\"scotty76\".<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Logical Operators<\/strong><\/h4>\n\n\n\n<p>Logical operators in MongoDB are used to combine multiple query clauses. They are powerful tools that allow you to build complex queries.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$and<\/code> (Logical AND)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Joins query clauses with a logical AND, requiring all clauses to be true.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$and\": &#91;\n    {\n      \"meta_data.age\": {\n        \"$gt\": 25\n      }\n    },\n    {\n      \"username\": \"scotty76\"\n    }\n  ]\n})\n\/\/ Finds users who are older than 25 and have a username of \"scotty76\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$or<\/code> (Logical OR)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Joins query clauses with a logical OR, requiring at least one clause to be true.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$or\": &#91;\n    {\n      \"username\": \"scotty76\"\n    },\n    {\n      \"meta_data.age\": {\n        \"$lt\": 30\n      }\n    }\n  ]\n})\n\/\/ Finds users who have a username of \"scotty76\" or are younger than 30.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$not<\/code> (Logical NOT)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Inverts the effect of a query expression, matching documents that do not meet the condition.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    \"$not\": {\n      \"$gt\": 30\n    }\n  }\n})\n\/\/ Finds users who are not older than 30.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$nor<\/code>&nbsp;(Logical NOR)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Joins query clauses with a logical NOR, requiring all clauses to be false.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$nor\": &#91;\n    {\n      \"username\": \"scotty76\"\n    },\n    {\n      \"meta_data.age\": {\n        \"$gt\": 30\n      }\n    }\n  ]\n})\n\/\/ Finds users who do not\n\/\/ have a username of \"scotty76\" and are not older than 30.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Element Operators<\/strong><\/h4>\n\n\n\n<p>Element operators are used to check the existence of fields and their types in documents.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$exists<\/code>&nbsp;(Field Exists)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents that have the specified field.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"email\": {\n    \"$exists\": true\n  }\n})\n\/\/ Finds users who have an email field.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$type<\/code>&nbsp;(Type)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Selects documents if a field is of the specified BSON type.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"username\": {\n    \"$type\": \"string\"\n  }\n})\n\/\/ Finds users where the username field is of type string.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Evaluation Operators<\/strong><\/h4>\n\n\n\n<p>Evaluation operators allow you to perform calculations, validate data, or match fields against complex conditions.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$expr<\/code>&nbsp;(Evaluate a condition)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Allows the use of aggregation expressions in the query language.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$expr\": {\n    \"$gt\": &#91;\n      \"$meta_data.enabled_on\",\n      \"$meta_data.dob\"\n    ]\n  }\n})\n\/\/ Finds users where enabled_on is greater than dob.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$jsonSchema<\/code>&nbsp;(JSON Schema Validation)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Validates documents against the specified JSON schema.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$jsonSchema\": {\n    \"bsonType\": \"object\",\n    \"required\": &#91;\n      \"username\",\n      \"meta_data.age\"\n    ],\n    \"properties\": {\n      \"username\": {\n        \"bsonType\": \"string\"\n      },\n      \"meta_data.age\": {\n        \"bsonType\": \"number\",\n        \"minimum\": 0\n      }\n    }\n  }\n})\n\/\/ Finds users that match the JSON schema.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$mod<\/code>&nbsp;(Modulo)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Performs a modulo operation on the value of a field and selects documents with a specified result.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    $mod: &#91;\n      4,\n      0\n    ]\n  }\n})\n\/\/ Finds users items where age is divisible by 4.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$regex<\/code>&nbsp;(Regular Expression)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents that satisfy a regular expression.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"name.first_name\": {\n    $regex: \"^Cand\"\n  }\n})\n\/\/ Finds users whose name starts with \"Cand\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$text<\/code>&nbsp;(Text Search)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Performs a text search on the content of the fields indexed with a text index.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$text\": {\n    \"$search\": \"Savannah\"\n  }\n})\n\/\/ Finds users that contain the term \"Savannah\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$where<\/code>&nbsp;(JavaScript expression)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents that satisfy a JavaScript expression.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$where\": \"this.meta_data.age &gt; 30\"\n})\n\/\/ Finds users who are older than 30 using a JavaScript expression.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Array Operators<\/strong><\/h4>\n\n\n\n<p>Array operators allow you to perform operations on array fields within documents.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$all<\/code>&nbsp;(Match all elements in an array)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches arrays that contain all the specified elements.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"dept\": {\n    \"$all\": &#91;\n      \"development\",\n      \"hr\"\n    ]\n  }\n})\n\/\/ Finds users that have both \"development\" and \"hr\" dept.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$elemMatch<\/code>&nbsp;(Element Match)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents that contain an array field with at least one element that matches all the specified criteria.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"address\": {\n    \"$elemMatch\": {\n      \"state\": \"New Jersey\",\n      \"country\": \"Austria\"\n    }\n  }\n})\n\/\/ Finds users who has state New Jersey and Country Austria.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$size<\/code>&nbsp;(Array Size)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Selects documents if the array field is a specified size.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"dept\": {\n    \"$size\": 2\n  }\n})\n\/\/ Finds users that have exactly two dept.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Bitwise Operators<\/strong><\/h4>\n\n\n\n<p>Bitwise operators are used to perform bit-level operations on numeric fields.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$bitsAllClear<\/code>&nbsp;(Bits All Clear)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the specified bit positions are clear (i.e., 0).<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"flags\": {\n    \"$bitsAllClear\": 3\n  }\n})\n\/\/ Finds users where the first two bits of flags are clear.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$bitsAllSet<\/code>&nbsp;(Bits All Set)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where the specified bit positions are set (i.e., 1).<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"flags\": {\n    \"$bitsAllSet\": 5\n  }\n})\n\/\/ Finds users where the first and third bits of flags are set.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$bitsAnyClear<\/code>&nbsp;(Bits Any Clear)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where any of the specified bit positions are clear.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"flags\": {\n    \"$bitsAnyClear\": 4\n  }\n})\n\/\/ Finds users where any of the specified bits are clear.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$bitsAnySet<\/code>&nbsp;(Bits Any Set)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents where any of the specified bit positions are set.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"flags\": {\n    \"$bitsAnySet\": 6\n  }\n})\n\/\/ Finds users where any of the specified bits are set.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Miscellaneous Operators<\/strong><\/h4>\n\n\n\n<p>Miscellaneous operators are less commonly used but provide additional functionality in specific scenarios.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$comment<\/code>&nbsp;(Add comments to query)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Attaches a comment to a query for later reference in logs and profiling.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    \"$gt\": 25\n  }\n}).comment(\"Finding users older than 25\")\n\/\/ Adds a comment to the query for logging or profiling purposes.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$rand<\/code>&nbsp;(Random Number)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Matches documents in a randomized order.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"meta_data.age\": {\n    \"$gt\": 25\n  }\n}).sort({ \"$rand\": { } })\n\/\/ Finds users older than 25 in a random order.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Projection Operators<\/h3>\n\n\n\n<p>Projection operators in MongoDB allow you to control which fields are returned in the query results. They help you include or exclude specific fields, project specific array elements, and even manipulate the data returned from the database.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Include\/Exclude Fields<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong><code>1<\/code>&nbsp;(Include Field)<\/strong>&nbsp;and <strong><code>0<\/code>&nbsp;(Exclude Field)<\/strong><\/li>\n\n\n\n<li><strong>Description<\/strong>: Use&nbsp;<code>1<\/code>&nbsp;to include a field and&nbsp;<code>0<\/code>&nbsp;to exclude a field from the results.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({},\n{\n  \"name.first_name\": 1,\n  \"email\": 1,\n  \"_id\": 0\n})\n\/\/ Returns only the first_name and email fields, excluding the _id field.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$<\/code>&nbsp;(Positional Operator)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Projects the first array element that matches the query condition.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"dept\": {\n    \"$eq\": \"hr\"\n  }\n},\n{\n  \"dept.$\": 1\n})\n\/\/ Returns the first dept in the dept array that is equal to \"hr\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$elemMatch<\/code>&nbsp;(Projection with Element Match)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Projects the first matching element from an array.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"dept\": {\n    \"$elemMatch\": {\n      \"$eq\": \"management\"\n    }\n  }\n},\n{\n  \"dept\": 1\n})\n\/\/ Projects the first dept in the dept array that is equal to \"management\".<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$meta<\/code>&nbsp;(Metadata Projection)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Projects metadata like text score or search highlights.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({\n  \"$text\": {\n    \"$search\": \"MongoDB\"\n  }\n},\n{\n  \"score\": {\n    \"$meta\": \"textScore\"\n  }\n})\n\/\/ Projects the text search score alongside the article data.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$slice<\/code>&nbsp;(Limit Array Elements)<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Description<\/strong>: Limits the number of array elements displayed in the results.<\/li>\n\n\n\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({},\n{\n  \"dept\": {\n    \"$slice\": 1\n  }\n})\n\/\/ Returns only the first dept from the dept array.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Final Thoughts<\/strong><\/h3>\n\n\n\n<p>Understanding MongoDB&#8217;s query and projection operators is crucial for writing efficient and effective queries. By mastering these operators, you can filter, combine, and project data with precision, enabling you to retrieve exactly what you need from your MongoDB collections.<\/p>\n\n\n\n<p>Whether you&#8217;re filtering documents, performing complex logical operations, or refining the output of your queries, these operators provide the flexibility and power necessary to handle a wide range of data retrieval scenarios.<\/p>\n\n\n\n<p>As with all powerful tools, practice is key. Experiment with these operators in your MongoDB environment, and you&#8217;ll soon find yourself crafting more sophisticated and optimized queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB is one of the most commonly used NoSQL databases. Query operators and projection operators in MongoDB offer fine control over which documents are retrieved and what format to apply to the resulting data set. We will shortly cover the different types of query and projection operators that are available within MongoDB. From filtering documents [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":370,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[6,5,1],"tags":[27,32],"class_list":["post-346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-development","category-technology","tag-database","tag-mongo"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/comments?post=346"}],"version-history":[{"count":25,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/posts\/346\/revisions\/372"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/media\/370"}],"wp:attachment":[{"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/media?parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/categories?post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codebounce.debojyotichatterjee.com\/index.php\/wp-json\/wp\/v2\/tags?post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}