Skip to content
Fix Code Error

How to query MongoDB with “like”?

March 13, 2021 by Code Error
Posted By: Anonymous

I want to query something with SQL’s like query:

SELECT * FROM users  WHERE name LIKE '%m%'

How to do I achieve the same in MongoDB? I can’t find an operator for like in the documentation.

Solution

That would have to be:

db.users.find({"name": /.*m.*/})

or, similar:

db.users.find({"name": /m/})

You’re looking for something that contains “m” somewhere (SQL’s ‘%‘ operator is equivalent to Regexp’s ‘.*‘), not something that has “m” anchored to the beginning of the string.

note: mongodb uses regular expressions which are more powerful than “LIKE” in sql. With regular expressions you can create any pattern that you imagine.

For more info on regular expressions refer to this link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Answered By: Anonymous

Related Articles

  • Reference - What does this regex mean?
  • What does this symbol mean in JavaScript?
  • What are the undocumented features and limitations…
  • How to use Regular Expressions (Regex) in Microsoft…
  • Problems Installing CRA & NextJS from NPM…
  • What is your most productive shortcut with Vim?
  • no match for ‘operator
  • What is an optional value in Swift?
  • What does "Fatal error: Unexpectedly found nil while…
  • How do you access the matched groups in a JavaScript…
  • How to use Elasticsearch with MongoDB?
  • Update MongoDB field using value of another field
  • How to stop mongo DB in one command
  • shell script. how to extract string using regular…
  • data.table vs dplyr: can one do something well the…
  • How to grep (search) committed code in the Git history
  • Smart way to truncate long strings
  • How to split a string in Java
  • Remove accents/diacritics in a string in JavaScript
  • Memcached vs. Redis?
  • What is the difference between re.search and re.match?
  • Unable to create/open lock file: /data/mongod.lock…
  • Couldn't connect to server 127.0.0.1:27017
  • What version of MongoDB is installed on Ubuntu
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • java.util.regex - importance of Pattern.compile()?
  • Binding complex object to a component
  • Difference between / and /* in servlet mapping url pattern
  • How can I parse a CSV string with JavaScript, which…
  • How to filter a RecyclerView with a SearchView
  • SQL query return data from multiple tables
  • How to validate an email address using a regular expression?
  • Reference — What does this symbol mean in PHP?
  • The definitive guide to form-based website authentication
  • Logging best practices
  • How do I obtain a Query Execution Plan in SQL Server?
  • How to use module.exports in Next.js
  • how to set mongod --dbpath
  • How do you parse and process HTML/XML in PHP?
  • mongodb group values by multiple fields
  • How to use Servlets and Ajax?
  • MySQL vs MongoDB 1000 reads
  • What is a NullReferenceException, and how do I fix it?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Where and why do I have to put the "template" and…
  • How do I count unique visitors to my site?
  • Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'?,./
  • Rails wrong number of arguments error when…
  • How to generate a random string of a fixed length in Go?
  • Unable to find module mongodb in simple node.js app
  • What's the difference between eval, exec, and compile?
  • Applying design patterns whilst collaborating with…
  • Find CRLF in Notepad++
  • Is there a regular expression to detect a valid…
  • Entity Framework LINQ complex query - combine…
  • Searching for text inside nested object (Backbone.js…
  • Vue js vuecli3 application does not work in ie11…
  • MONGODB-ORG not running on UBUNTU 20.04
  • What's the best way of scraping data from a website?
  • List tables in a PostgreSQL schema
  • How to do a regular expression replace in MySQL?
  • How to download and save an image in Android
  • How to paste yanked text into the Vim command line
  • How to implement an STL-style iterator and avoid…
  • Use dynamic (variable) string as regex pattern in JavaScript
  • Database development mistakes made by application developers
  • How do you match strings in custom Instruments…
  • What is the copy-and-swap idiom?
  • meta_query search names when they have middle…
  • Callback functions in C++
  • Why does C++ code for testing the Collatz conjecture…
  • Overloading operators in typedef structs (c++)
  • For-each over an array in JavaScript
  • Start redis-server with config file
  • Fastest way to check a string contain another…
  • Python re.sub(): how to substitute all 'u' or 'U's…
  • Why doesn't [01-12] range work as expected?
  • When to use MongoDB or other document oriented…
  • How to check if a string contains only digits in Java
  • How to validate email id in angularJs using ng-pattern
  • How to create range in Swift?
  • Regexp not matching string with [] and / in tcl
  • How can I find the product GUID of an installed MSI setup?
  • Giving multiple URL patterns to Servlet Filter
  • Getting started with Haskell
  • MongoDB vs. Cassandra
  • Fastest way to iterate over all the chars in a String
  • Creating a singleton in Python
  • Regex Named Groups in Java
  • Regular expression for letters, numbers and - _
  • Using Auto Layout in UITableView for dynamic cell…
  • How to listen for changes to a MongoDB collection?
  • RegEx match open tags except XHTML self-contained tags
  • Mongodb use find (not aggregation) to get only one…
  • Getting parts of a URL (Regex)
  • Java SimpleDateFormat for time zone with a colon separator?
  • What is The Rule of Three?
  • Regex match first character once, followed by…
  • C++ template,typename and operator
  • Android Gradle plugin 0.7.0: "duplicate files during…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Storing Objects in HTML5 localStorage

Next Post:

How do I convert a float number to a whole number in JavaScript?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error