{"id":2785,"date":"2021-09-07T08:37:40","date_gmt":"2021-09-07T03:07:40","guid":{"rendered":"https:\/\/explore.techenutia.com\/?p=2785"},"modified":"2021-09-07T08:37:40","modified_gmt":"2021-09-07T03:07:40","slug":"or-operator-in-mongoose","status":"publish","type":"post","link":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/","title":{"rendered":"$or Operator in Mongoose"},"content":{"rendered":"\n<p>$or operator in <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/operator\/query\/or\/\" data-type=\"URL\" data-id=\"https:\/\/docs.mongodb.com\/manual\/reference\/operator\/query\/or\/\" target=\"_blank\" rel=\"noreferrer noopener\">mongoose<\/a> is used to perform a <strong>logical OR operation<\/strong> on an array of expressions. It helps you in situations when you need to find documents with multiple conditions. Such as creating a search with multiple parameters. It accepts an array of expressions and returns all the documents that satisfy either of the expressions in the array. <\/p>\n\n\n\n<p>Let&#8217;s create a mongoose search query that will <strong>search a user based on email, mobile, and name. <\/strong>The function will receive a query parameter in the body and our goal is to return all the users whose <strong>email or mobile or name matches with the query parameter<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const search = async (req, res, next) => {\nconst {query} = req.body; \/\/destructring query from body\nconst foundDocs = await userSchema.find({\n$or: &#91;\n{email: query}, \n{mobile: query}, \n{name: query}\n]\n});\nres.send({\ncode: 200,\ndata: foundDocs,\nmessage: \"Found Documents\"\n});\n}<\/code><\/pre>\n\n\n\n<p>So, that&#8217;s a simple mongoose search query. This will return only the matching documents. But what if you need a search that searches for all documents that contain the query word? <strong>For example, if query word is equal to you should get all the documents whose email or mobile or name contains a? The simple answer is using regex. Here is a little example <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const search = async (req, res, next) => {\nconst {query} = req.body; \/\/destructring query from body\nconst foundDocs = await userSchema.find({\n$or: &#91;\n{email: new RegExp(query, 'i')},\n{mobile: new RegExp(query, 'i')}, \n{name: new RegExp(query, 'i')}\n]\n});\nres.send({\ncode: 200,\ndata: foundDocs,\nmessage: \"Found Documents\"\n});\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$or operator in mongoose is used to perform a logical OR operation on an array of expressions. It helps you in situations when you need to find documents with multiple conditions. Such as creating a search with multiple parameters. It accepts an array of expressions and returns all the documents&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\">Read the post<span class=\"screen-reader-text\">$or Operator in Mongoose<\/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":[],"class_list":["post-2785","post","type-post","status-publish","format-standard","hentry","category-infinity-fitness","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>$or Operator in Mongoose | ManOrInfinity<\/title>\n<meta name=\"description\" content=\"how to create a search in nodejs? create search using mongoose? mongoose query for searching? easiest way to create a search in nodejs\" \/>\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\/09\/07\/or-operator-in-mongoose\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"$or Operator in Mongoose\" \/>\n<meta property=\"og:url\" content=\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\" \/>\n<meta property=\"og:site_name\" content=\"ManOrInfinity\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-07T03:07:40+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\/09\/07\/or-operator-in-mongoose\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\"},\"author\":{\"name\":\"manorinfinity\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"headline\":\"$or Operator in Mongoose\",\"datePublished\":\"2021-09-07T03:07:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\"},\"wordCount\":174,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901\"},\"articleSection\":[\"Infinity Fitness\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\",\"url\":\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\",\"name\":\"$or Operator in Mongoose | ManOrInfinity\",\"isPartOf\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/#website\"},\"datePublished\":\"2021-09-07T03:07:40+00:00\",\"description\":\"how to create a search in nodejs? create search using mongoose? mongoose query for searching? easiest way to create a search in nodejs\",\"breadcrumb\":{\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/manorinfinity.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"$or Operator in Mongoose\"}]},{\"@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":"$or Operator in Mongoose | ManOrInfinity","description":"how to create a search in nodejs? create search using mongoose? mongoose query for searching? easiest way to create a search in nodejs","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\/09\/07\/or-operator-in-mongoose\/","og_locale":"en_US","og_type":"article","og_title":"$or Operator in Mongoose","og_url":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/","og_site_name":"ManOrInfinity","article_published_time":"2021-09-07T03:07:40+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\/09\/07\/or-operator-in-mongoose\/#article","isPartOf":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/"},"author":{"name":"manorinfinity","@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"headline":"$or Operator in Mongoose","datePublished":"2021-09-07T03:07:40+00:00","mainEntityOfPage":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/"},"wordCount":174,"commentCount":0,"publisher":{"@id":"https:\/\/manorinfinity.com\/blog\/#\/schema\/person\/1172b1895b5eb7e49cc8640e49255901"},"articleSection":["Infinity Fitness"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/","url":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/","name":"$or Operator in Mongoose | ManOrInfinity","isPartOf":{"@id":"https:\/\/manorinfinity.com\/blog\/#website"},"datePublished":"2021-09-07T03:07:40+00:00","description":"how to create a search in nodejs? create search using mongoose? mongoose query for searching? easiest way to create a search in nodejs","breadcrumb":{"@id":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/manorinfinity.com\/blog\/2021\/09\/07\/or-operator-in-mongoose\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/manorinfinity.com\/blog\/"},{"@type":"ListItem","position":2,"name":"$or Operator in Mongoose"}]},{"@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\/2785","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=2785"}],"version-history":[{"count":0,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/posts\/2785\/revisions"}],"wp:attachment":[{"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/media?parent=2785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/categories?post=2785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manorinfinity.com\/blog\/wp-json\/wp\/v2\/tags?post=2785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}