{"id":2708,"date":"2021-07-05T12:42:16","date_gmt":"2021-07-05T12:42:16","guid":{"rendered":"https:\/\/explore.techenutia.com\/?p=2708"},"modified":"2022-12-28T15:40:44","modified_gmt":"2022-12-28T15:40:44","slug":"how-to-get-documents-from-mongodb-except-one-nin","status":"publish","type":"post","link":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/","title":{"rendered":"How To Get Documents From MongoDB except One: $nin"},"content":{"rendered":"\n<p>In situations where you need to get documents excluding some specific values, as a beginner, you will extract all documents and use javascript to filter. But that&#8217;s not the ideal way to do this. MongoDB gifted us with a really <a href=\"https:\/\/explore.techenutia.com\/2021\/06\/04\/limit-sort-skip-pagination-functions-in-mongodb\/\">powerful operator<\/a> that can help us make a <a href=\"https:\/\/explore.techenutia.com\/2021\/06\/10\/what-is-population-in-mongoose\/\">mongo<\/a> query for the same. Benefits of using the operator include less code, better performance. The operator is $nin<\/p>\n\n\n\n<p>$nin accepts an array of values for a particular key in the document schema. All the documents that will match the keys in the $nin array will be excluded from the results. Let&#8217;s take an example: we have a schema Exclusion and we want to get all the documents in this document except the ones whose value key matches our array of values. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Exclusion.find(value: {$nin: &#91;1,2]})<\/code><\/pre>\n\n\n\n<p>This is a simple use case of fetching data. But what if you want to update some data in the collection except some documents. Here is a query to explain just that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Exclusion.update({name: {$nin:&#91;\"Amigo\", \"Adios\"]}, {updateField: \"New Value\"})<\/code><\/pre>\n\n\n\n<p>That&#8217;s it for this lesson. If you have any queries feel free to reach out on social media or comment below expect a reply with 30min if I am not asleep\ud83d\ude02<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In situations where you need to get documents excluding some specific values, as a beginner, you will extract all documents and use javascript to filter. But that&#8217;s not the ideal way to do this. MongoDB gifted us with a really powerful operator that can help us make a mongo query&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\">Read the post<span class=\"screen-reader-text\">How To Get Documents From MongoDB except One: $nin<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[244,753,778,779],"class_list":["post-2708","post","type-post","status-publish","format-standard","hentry","category-infinity-fitness","tag-nin","tag-mern-stack","tag-mongodb","tag-mongoose","excerpt","zoom","full-without-featured","even","excerpt-0"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.12 (Yoast SEO v26.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How To Get Documents From MongoDB except One: $nin | ManOrInfinity<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Get Documents From MongoDB except One: $nin\" \/>\n<meta property=\"og:description\" content=\"In situations where you need to get documents excluding some specific values, as a beginner, you will extract all documents and use javascript to filter. But that&#8217;s not the ideal way to do this. MongoDB gifted us with a really powerful operator that can help us make a mongo query&#8230;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\" \/>\n<meta property=\"og:site_name\" content=\"ManOrInfinity\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-05T12:42:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-28T15:40:44+00:00\" \/>\n<meta name=\"author\" content=\"manorinfinity\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@manorinfinity\" \/>\n<meta name=\"twitter:site\" content=\"@manorinfinity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"manorinfinity\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\"},\"author\":{\"name\":\"manorinfinity\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"headline\":\"How To Get Documents From MongoDB except One: $nin\",\"datePublished\":\"2021-07-05T12:42:16+00:00\",\"dateModified\":\"2022-12-28T15:40:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\"},\"wordCount\":202,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"keywords\":[\"$nin\",\"mern stack\",\"mongodb\",\"mongoose\"],\"articleSection\":[\"Infinity Fitness\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\",\"url\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\",\"name\":\"How To Get Documents From MongoDB except One: $nin | ManOrInfinity\",\"isPartOf\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#website\"},\"datePublished\":\"2021-07-05T12:42:16+00:00\",\"dateModified\":\"2022-12-28T15:40:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/manorinfinity.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Get Documents From MongoDB except One: $nin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#website\",\"url\":\"https:\/\/manorinfinity.com\/blog\/\",\"name\":\"ManOrInfinity\",\"description\":\"Thrive towards greatness\",\"publisher\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/manorinfinity.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\",\"name\":\"manorinfinity\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png\",\"contentUrl\":\"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png\",\"width\":282,\"height\":260,\"caption\":\"manorinfinity\"},\"logo\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/\"},\"description\":\"Complex Problem Solver, Outloud Thinker, An Outstanding Writer, and a very curious human being\",\"sameAs\":[\"http:\/\/manorinfinity.com\"],\"url\":\"https:\/\/manorinfinity.com\/blog\/author\/manorinfinity\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How To Get Documents From MongoDB except One: $nin | ManOrInfinity","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/","og_locale":"en_US","og_type":"article","og_title":"How To Get Documents From MongoDB except One: $nin","og_description":"In situations where you need to get documents excluding some specific values, as a beginner, you will extract all documents and use javascript to filter. But that&#8217;s not the ideal way to do this. MongoDB gifted us with a really powerful operator that can help us make a mongo query&#8230;","og_url":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/","og_site_name":"ManOrInfinity","article_published_time":"2021-07-05T12:42:16+00:00","article_modified_time":"2022-12-28T15:40:44+00:00","author":"manorinfinity","twitter_card":"summary_large_image","twitter_creator":"@manorinfinity","twitter_site":"@manorinfinity","twitter_misc":{"Written by":"manorinfinity","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#article","isPartOf":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/"},"author":{"name":"manorinfinity","@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"headline":"How To Get Documents From MongoDB except One: $nin","datePublished":"2021-07-05T12:42:16+00:00","dateModified":"2022-12-28T15:40:44+00:00","mainEntityOfPage":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/"},"wordCount":202,"commentCount":0,"publisher":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"keywords":["$nin","mern stack","mongodb","mongoose"],"articleSection":["Infinity Fitness"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/","url":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/","name":"How To Get Documents From MongoDB except One: $nin | ManOrInfinity","isPartOf":{"@id":"https:\/\/manorinfinity.com\/blog\/#website"},"datePublished":"2021-07-05T12:42:16+00:00","dateModified":"2022-12-28T15:40:44+00:00","breadcrumb":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/manorinfinity.com\/blog\/2021\/07\/05\/how-to-get-documents-from-mongodb-except-one-nin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/manorinfinity.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Get Documents From MongoDB except One: $nin"}]},{"@type":"WebSite","@id":"https:\/\/manorinfinity.com\/blog\/#website","url":"https:\/\/manorinfinity.com\/blog\/","name":"ManOrInfinity","description":"Thrive towards greatness","publisher":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/manorinfinity.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901","name":"manorinfinity","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png","contentUrl":"http:\/\/manorinfinity.com\/wp-content\/uploads\/2023\/06\/moi-logo.png","width":282,"height":260,"caption":"manorinfinity"},"logo":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/image\/"},"description":"Complex Problem Solver, Outloud Thinker, An Outstanding Writer, and a very curious human being","sameAs":["http:\/\/manorinfinity.com"],"url":"https:\/\/manorinfinity.com\/blog\/author\/manorinfinity\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts\/2708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/comments?post=2708"}],"version-history":[{"count":1,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts\/2708\/revisions"}],"predecessor-version":[{"id":3375,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts\/2708\/revisions\/3375"}],"wp:attachment":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/media?parent=2708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/categories?post=2708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/tags?post=2708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}