Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a field contains a string

Tags:

mongodb

I'm looking for an operator, which allows me to check, if the value of a field contains a certain string.

Something like:

db.users.findOne({$contains:{"username":"son"}}) 

Is that possible?

like image 314
johnny Avatar asked May 15 '12 23:05

johnny


People also ask

How do you check if a field contains a string in Java?

The third method you can use to check if one String contains another in Java is contains() method of String. This method returns true if a substring is present in String, or false otherwise. The only difference between contains() and indexOf() is that the former returns a boolean value while later returns an int value.

How do I search for a part of a string in MongoDB?

MongoDB gives the functionality to search a pattern in a string through a query by writing a regular expression. The regular expression capabilities are used for pattern matching strings in queries and for that, we use the $regex operator. Syntax: db.

What is a string in MongoDB?

MongoDB connection string is defined as connection format to join the MongoDB database server, we have using username, hostname, password, and port parameter to connect the database server. Without a connection string, we cannot connect to the database server, we need a connection string to connect the database server.

How do I query in MongoDB?

The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.

Is it possible to check if a field value is like string?

I know it is possible to check if a field value is like a string like this: But what I want is to check if a string contains the filed value. If I have a string like this: "John, Smith, ", I wanted to match users with username "John" and "Smith".

How do you check if a string is a substring?

Substring: the specific text you want to search in the cell. Text: the cell or text string you want to check if contains a specific text (the argument substring). This formula returns a logical value. If the cell contains the substring, the formula returns TRUE, or it returns FALSE.

How to check if a string contains a numeric value in Excel?

Returning TRUE indicates the cell contains a numeric value, or return FALSE. Here ISNUMBER (SEARCH (C3,B3)) will check if the result of SEARCH function is a numeric value. Substring: the specific text you want to search in the cell.

How to check if cell contains a specific text in Excel?

Text: the cell or text string you want to check if contains a specific text (the argument substring). This formula returns a logical value. If the cell contains the substring, the formula returns TRUE, or it returns FALSE. Here you want to check if cell B4 contains the text in C4, use below formula Press Enter key to check.


1 Answers

You can do it with the following code.

db.users.findOne({"username" : {$regex : "son"}}); 
like image 59
Parvin Gasimzade Avatar answered Oct 11 '22 03:10

Parvin Gasimzade