mongodb find a pattern of an input - regex

I need to find all the results that start with certain input for example for the inputs: "Paul", "pau", "paul Gr", "Paul Green", "Paul Gree" , "Pel", "pele", "joh","john" etc.. The search has to be case insensive..
it suppose to return all of these(the input search string is at least 3 characters long):
[
{
"_id": ObjectId("5e6ffe413f71835ae3aa4b60"),
"f": "Paul",
"id": 11811,
"l": "Pelè",
"r": 64
},
{
"_id": ObjectId("5e6ffe413f71835ae3aa4b65"),
"f": "paul",
"id": 11811,
"l": "walker",
"r": 64
},
{
"_id": ObjectId("5e6ffe413f71835ae3aa4b66"),
"f": "johnny",
"id": 11811,
"l": "Green",
"r": 64
}
]
tried to do the following:
contain_searched_term_players = list(db.players_collection.find({'$or': [{'f': {'$regex': searched_player_name_string, '$options': 'i'}},
{'l': {'$regex': searched_player_name_string, '$options': 'i'}},
{'c': {'$regex': searched_player_name_string, '$options': 'i'}}]}).sort([{'r', -1}])
but it doesnt work for "Paul Green"
searched_player_name_string is the given input(the inputs above, for example Paul Green)

You need to provide correct Regex for query condition
^(Paul Green|Paul Gree|Paul|paul|pau|Gr|pele|Pel|john|joh)
RegexPlayground
searched_player_name_string = "^(Paul Green|Paul Gree|Paul|paul|pau|Gr|pele|Pel|john|joh)"
result_cursor = db.players_collection.find({
"$or": [
{
"f": {
"$regex": searched_player_name_string,
"$options": "i"
}
},
{
"l": {
"$regex": searched_player_name_string,
"$options": "i"
}
},
{
"c": {
"$regex": searched_player_name_string,
"$options": "i"
}
}
]
})
searched_player_name_string = list(result_cursor)
MongoPlayground

Split your input in separate strings, run the query on each and append the results together (checking first it's not already found), Finally sort the results:
searched_player_name_string = 'Paul Green'
found_players = []
for regex in searched_player_name_string.split():
contain_searched_term_players = db.players_collection.find({'$or': [{'f': {'$regex': regex, '$options': 'i'}},
{'l': {'$regex': regex, '$options': 'i'}},
{'c': {'$regex': regex, '$options': 'i'}}]})
for player in contain_searched_term_players:
# The next line avoids creating duplicate answers if there are multiple matches for the same player
if player['_id'] not in [ o['_id'] for o in found_players ]:
found_players.append(player)
# Sort the output by "r" - highest first
pprint.pprint(sorted(found_players, key=lambda o: o['r'], reverse=True))

Related

How to find middle of the string, next to space and dot in MongoDB

[
{
"Name": "Dr.Soma",
"Email": "drsoma#gmail.com",
"MobNo": 111111111
},
{
"Name": "Bootha Ganesh",
"Email": "boothaganesg#gmail.com",
"MobNo": 222222222
},
{
"Name": "Steven",
"Email": "steven#gmail.com",
"MobNo": 333333333
},
{
"Name": "Dr.Anbarasi",
"Email": "anbarasi#gmail.com",
"MobNo": 4444444444
}
]
I try this to using find regex
db.details.find({Name:{$regex:/steven/i}})
output:
{
"Name": "Steven",
"Email": "steven#gmail.com",
"MobNo": 333333333
}
How to find data Name dot(.) after Soma & Space after Ganesh
Excepted Output
If I find Name Ganesh,I need
{
"Name": "Bootha Ganesh",
"Email": "boothaganesg#gmail.com",
"MobNo": 222222222
}
If I find Name small s or capital S ,I need
{
"Name": "Dr.Soma",
"Email": "drsoma#gmail.com",
"MobNo": 111111111
}
No Need Name Dr.Anbarasi data
db.collection.find({"Name": {'$regex': /\bsoma[a-zA-Z0-9]*/gi}})
\b assert position at a word boundary: (^\w | \w$ | \W\w|\w\W)
soma-for searching value
[a-zA-Z0-9]-word characters
*-to entire string
db.collection.find({ Name: { $regex: "Soma" } })
mongoplayground
db.collection.find({ Name: { $regex: ".Soma" } })
mongoplayground
db.collection.find({ Name: { $regex: " Ganesh" } })
mongoplayground

MongoDB - Find numbers that starts with a string

I'm trying to make a query that gets all the prices that starts with '12'.
I have a collection like this:
{
"place": "Costa Rica",
"name": "Villa Lapas",
"price": 1353,
},
{
"place": "Costa Rica",
"name": "Hotel NWS",
"price": 1948,
},
{
"place": "Costa Rica",
"name": "Hotel Papaya",
"price": 1283,
},
{
"place": "Costa Rica",
"name": "Hostal Serine",
"price": 1248,
},
And I want my results like this:
{
'prices': [
1248,
1283
]
}
I'm converting all the prices to string in order to use a regex function. But I don't understand very well how to use the regex in my query.
My query returns:
{ "prices" : null }
{ "prices" : null }
Could someone please guide me? :)
db.collection.aggregate([
{'$project': {
'_id': 0,
'price': {'$toString': '$price'}
}},
{'$project': {
'prices': {'$regexFind': { 'input': "$price", 'regex': '^12' }}
}}
]).pretty();
You are almost correct.
db.test.aggregate([
{'$project': {
'_id': 0,
'prices': {'$toString': '$price'}
^^^ -> I meant this
}},
{'$match': {
'prices': {'$regex': '^12' }
^^^ -> same here
}}
])
You need to use $match with $regex which yields the result as you expected.
If you use regexFind, it works on all matching docs and returns null where input doesn't match the pattern
And
In the first project you have price instead prices. If you refer the first project name in the second project, then pipeline matches.

MongoDB Aggregate Regex Match or Full Text Search returns whole Document

Ex. Record
[
{
"_id": "5528cfd2e71144e020cb6494",
"__v": 11,
"Product": [
{
"_id": "5528cfd2e71144e020cb6495",
"isFav": true,
"quantity": 27,
"price": 148,
"description": "100g",
"brand": "JaldiLa",
"name": "Grapes",
"sku": "GRP"
},
{
"_id": "552963ed63d867b81e18d357",
"isFav": false,
"quantity": 13,
"price": 290,
"description": "100g",
"brand": "JaldiLa",
"name": "Apple",
"sku": "APL"
}
],
"brands": [
"Whole Foods",
"Costco",
"Bee's",
"Masons"
],
"sku": "FRT",
"name": "Fruits"
}
]
My Mongoose function to return query from AngularJS(http://localhost:8080/api/search?s=)
router.route('/search')
.get(function(req, res) {
Dept.aggregate(
{ $match: { $text: { $search: req.query.s } } },
{ $project : { name : 1, _id : 1, 'Product.name' : 1, 'Product._id' : 1} },
{ $unwind : "$Product" },
{ $group : {
_id : "$_id",
Category : { $addToSet : "$name"},
Product : { $push : "$Product"}
}}
)
});
RESULT: e.g. http://localhost:8080/api/search?s=Apple / Grape / Carrot, result is same for all.
[
{
"_id": "5528cfd2e71144e020cb6494",
"Category": ["Fruits"],
"Product": [
{
"_id": "5528cfd2e71144e020cb6495",
"name": "Grapes"
},
{
"_id": "552963ed63d867b81e18d357",
"name": "Apple"
},
{
"_id": "552e61920c530fb848c61510",
"name": "Carrots"
}
]
}
]
PROBLEM: On a query of "apple", it returns all objects within Product instead of just "grapes", i think maybe putting match after unwind would do the trick or $regex case
WHAT I WANT: e.g. for a searchString of "grape"
Also I want it to start sending results as soon as I send in the first two letters of my query.
[{
"_id": ["5528cfd2e71144e020cb6494"], //I want this in array as it messes my loop up
"Category": "Fruits", //Yes I do not want this in array like I'm getting in my resutls
"Product": [{
"_id": "5528cfd2e71144e020cb6495",
"name": "Grapes"
}]
}]
Thanks for being patient.
Use the following aggregation pipeline:
var search = "apple",
pipeline = [
{
"$match": {
"Product.name": { "$regex": search, "$options": "i" }
}
},
{
"$unwind": "$Product"
},
{
"$match": {
"Product.name": { "$regex": search, "$options": "i" }
}
},
{
"$project": {
"Category": "$name",
"Product._id": 1,
"Product.name": 1
}
}
];
db.collection.aggregate(pipeline);
With the above sample document and a regex (case-insensitive) search for "apple" on the name field of the Product array, the above aggregation pipeline produces the result:
Output:
/* 1 */
{
"result" : [
{
"_id" : "5528cfd2e71144e020cb6494",
"Product" : {
"_id" : "552963ed63d867b81e18d357",
"name" : "Apple"
},
"Category" : "Fruits"
}
],
"ok" : 1
}

How to use regex inside in query using morphia?

Mongodb allows regex expression of pattern /pattern/ without using $regex expression.
http://docs.mongodb.org/manual/reference/operator/query/in/
How can i do it using morphia ?
If i give Field criteria with field operator as in and value of type "java.util.regex.Pattern" then the equivalent query generated in
$in:[$regex: 'given pattern'] which wont return expected results at all.
Expectation: $in :[ /pattern1 here/,/pattern2 here/]
Actual using 'Pattern' object : $in : [$regex:/pattern1 here/,$regex:/pattern 2 here/]
I'm not entirely sure what to make of your code examples, but here's a working Morphia code snippet:
Pattern regexp = Pattern.compile("^" + email + "$", Pattern.CASE_INSENSITIVE);
mongoDatastore.find(EmployeeEntity.class).filter("email", regexp).get();
Note that this is really slow. It can't use an index and will always require a full collection scan, so avoid it at all cost!
Update: I've added a specific code example. The $in is not required to search inside an array. Simply use /^I/ as you would in string:
> db.profile.find()
{
"_id": ObjectId("54f3ac3fa63f282f56de64bd"),
"tags": [
"India",
"Australia",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ac4da63f282f56de64be"),
"tags": [
"Island",
"Antigua"
]
}
{
"_id": ObjectId("54f3ac5ca63f282f56de64bf"),
"tags": [
"Spain",
"Mexico"
]
}
{
"_id": ObjectId("54f3ac6da63f282f56de64c0"),
"tags": [
"Israel"
]
}
{
"_id": ObjectId("54f3ad17a63f282f56de64c1"),
"tags": [
"Germany",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ad56a63f282f56de64c2"),
"tags": [
"ireland"
]
}
> db.profile.find({ tags: /^I/ })
{
"_id": ObjectId("54f3ac3fa63f282f56de64bd"),
"tags": [
"India",
"Australia",
"Indonesia"
]
}
{
"_id": ObjectId("54f3ac4da63f282f56de64be"),
"tags": [
"Island",
"Antigua"
]
}
{
"_id": ObjectId("54f3ac6da63f282f56de64c0"),
"tags": [
"Israel"
]
}
{
"_id": ObjectId("54f3ad17a63f282f56de64c1"),
"tags": [
"Germany",
"Indonesia"
]
}
Note: The position in the array makes no difference, but the search is case sensitive. Use /^I/i if this is not desired or Pattern.CASE_INSENSITIVE in Java.
Single RegEx Filter
use .filter(), .criteria(), or .field()
query.filter("email", Pattern.compile("reg.*exp"));
// or
query.criteria("email").contains("reg.*exp");
// or
query.field("email").contains("reg.*exp");
Morphia converts this into:
find({"email": { $regex: "reg.*exp" } })
Multiple RegEx Filters
query.or(
query.criteria("email").contains("reg.*exp"),
query.criteria("email").contains("reg.*exp.*2"),
query.criteria("email").contains("reg.*exp.*3")
);
Morphia converts this into:
find({"$or" : [
{"email": {"$regex": "reg.*exp"}},
{"email": {"$regex": "reg.*exp.*2"}},
{"email": {"$regex": "reg.*exp.*3"}}
]
})
Unfortunately,
You cannot use $regex operator expressions inside an $in.
MongoDB Manual 3.4
Otherwise, we could do:
Pattern[] patterns = new Pattern[] {
Pattern.compile("reg.*exp"),
Pattern.compile("reg.*exp.*2"),
Pattern.compile("reg.*exp.*3"),
};
query.field().in(patterns);
hopefully, one day morphia will support that :)

using regex to remove the opening brackets of JSON data coldfusion

I am working with JSON data and response is coming very good
[
{
"s": "1",
"sent": "September, 11 2014 18:19:10 -0400",
"f": "user1",
"m": "the place",
"fr": "user2"
},
{
"s": "1",
"sent": "September, 11 2014 18:19:19 -0400",
"f": "user2",
"m": "that once decided",
"fr": "user1"
},
{
"s": "1",
"sent": "September, 11 2014 18:19:23 -0400",
"f": "user1",
"m": "on your side",
"fr": "user2"
},
{
"s": "1",
"sent": "September, 11 2014 18:19:43 -0400",
"f": "user2",
"m": "actually i moved the text",
"fr": "user1"
},
{
"s": "1",
"sent": "September, 11 2014 18:20:06 -0400",
"f": "user2",
"m": "nothing specific",
"fr": "user1"
}
]
I would like to remove the ending and starting [] from the JSON, currently I have to write two Replace to do it, but it works sometimes and sometimes it does not. What could be relevant regex to sort out this issue. please guide
regards
You can do it with one replace using a regex with an alternation (|) ^\s*\[|]\s*$. That says: Match any series of whitespace followed by [ from the beginning of the string (^\s*\[), or match ] followed by any series of whitespace at the end of the string ([\s*$) with nothing. Gratuitous online explanation of the regex. Use that with REReplace (for instance) with scope set to all and a blank string as the replacement.