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?
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.
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.
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.
The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.
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".
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.
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.
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.
You can do it with the following code.
db.users.findOne({"username" : {$regex : "son"}});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With