mongodb 查找输入的模式 [英] mongodb find a pattern of an input
问题描述
我需要找到以特定输入开头的所有结果,例如输入:Paul"、pau"、paul Gr"、Paul Green"、Paul Gree"、Pel"、pele""、"joh"、"john" 等.搜索必须区分大小写.它假设返回所有这些(输入的搜索字符串长度至少为 3 个字符):
<预><代码>[{"_id": ObjectId("5e6ffe413f71835ae3aa4b60"),"f": "保罗",身份证":11811,"l": "贝莱",r":64},{"_id": ObjectId("5e6ffe413f71835ae3aa4b65"),"f": "保罗",身份证":11811,"l": "步行者",r":64},{"_id": ObjectId("5e6ffe413f71835ae3aa4b66"),"f": "约翰尼",身份证":11811,"l": "绿色",r":64}]尝试执行以下操作:
contains_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}])
但它不适用于保罗格林"
searched_player_name_string 是给定的输入(上面的输入,例如 Paul Green)
您需要为查询条件提供正确的Regex
^(Paul Green|Paul Gree|Paul|paul|pau|Gr|pele|Pel|john|joh)
searched_player_name_string = "^(Paul Green|Paul Gree|Paul|paul|pau|Gr|pele|Pel|john|joh)"result_cursor = db.players_collection.find({$或":[{F": {"$regex": searched_player_name_string,"$options": "i"}},{我":{"$regex": searched_player_name_string,"$options": "i"}},{C": {"$regex": searched_player_name_string,"$options": "i"}}]})searched_player_name_string = 列表(result_cursor)
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)
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)
这篇关于mongodb 查找输入的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!