使用 django 和 mongoengine 的子句执行正则表达式查询
Performing regex Queries with in caluse with django and mongoengine
我有 JSON 个工作对象,其中工作有一个关键的调用技能,这是一个技能列表。例如
{
"_id" : ObjectId("552a42cd03462b0728feb180"),
"did" : "JB77FW6885MCCRGG30T",
"attributes" : {
"EducationRequired" : "Bachelor's Degree",
"ExperienceRequired" : "At least 3 year(s)",
"ExternalApplication" : "True",
"City" : "Houston",
"Company" : "IBM",
"DegreeRequired" : "4 Year Degree",
"Skills" : {
"Skill" : [
"apache hadoop",
"etl",
"geographic information system",
"cloud computing",
"mongodb",
"nosql",
"linux",
"spark (programming language)",
"java (programming language)",
"python (programming language)",
"sql",
"machine learning",
"data management",
"data integration"
]
}
}
}
{
"_id" : ObjectId("552a42cd03462b0728feb180"),
"did" : "JB77FW6885MCCRGG30T",
"attributes" : {
"EducationRequired" : "Bachelor's Degree",
"ExperienceRequired" : "At least 5 year(s)",
"ExternalApplication" : "True",
"City" : "Boston",
"Company" : "Microsoft",
"DegreeRequired" : "4 Year Degree",
"Skills" : {
"Skill" : [
"java (programming language)",
"python (programming language)",
"sql",
"machine learning",
"data management",
"data integration"
]
}
}
}
我想获得 'sql' 或 'java'
的工作
我有以下电话
skills = ['java','sql']
jobs = Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : skills}})
问题在于它没有 return 具有 'java' 的工作,因为在数据库中技能列表具有 'java (programming language)' 而不仅仅是 'java' .为了解决这个问题,我需要在查询中加入一个正则表达式。所以我尝试了以下方法,但其中 none 有效:
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['$/.*java.*/']}}) #ignoring sql for now
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['sql',re.compile('$/.*java.*/', re.IGNORECASE)]}})
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['sql',re.compile('/.*java.*/', re.IGNORECASE)]}})
对我应该做什么有什么想法吗?
http://docs.mongodb.org/manual/reference/operator/query/regex/#syntax-restrictions
您的正则表达式不正确。试试这些。
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : [/.*java.*/i]}}) #ignoring sql for now
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['sql',re.compile('.*java.*', re.IGNORECASE)]}})
我有 JSON 个工作对象,其中工作有一个关键的调用技能,这是一个技能列表。例如
{
"_id" : ObjectId("552a42cd03462b0728feb180"),
"did" : "JB77FW6885MCCRGG30T",
"attributes" : {
"EducationRequired" : "Bachelor's Degree",
"ExperienceRequired" : "At least 3 year(s)",
"ExternalApplication" : "True",
"City" : "Houston",
"Company" : "IBM",
"DegreeRequired" : "4 Year Degree",
"Skills" : {
"Skill" : [
"apache hadoop",
"etl",
"geographic information system",
"cloud computing",
"mongodb",
"nosql",
"linux",
"spark (programming language)",
"java (programming language)",
"python (programming language)",
"sql",
"machine learning",
"data management",
"data integration"
]
}
}
}
{
"_id" : ObjectId("552a42cd03462b0728feb180"),
"did" : "JB77FW6885MCCRGG30T",
"attributes" : {
"EducationRequired" : "Bachelor's Degree",
"ExperienceRequired" : "At least 5 year(s)",
"ExternalApplication" : "True",
"City" : "Boston",
"Company" : "Microsoft",
"DegreeRequired" : "4 Year Degree",
"Skills" : {
"Skill" : [
"java (programming language)",
"python (programming language)",
"sql",
"machine learning",
"data management",
"data integration"
]
}
}
}
我想获得 'sql' 或 'java'
的工作我有以下电话
skills = ['java','sql']
jobs = Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : skills}})
问题在于它没有 return 具有 'java' 的工作,因为在数据库中技能列表具有 'java (programming language)' 而不仅仅是 'java' .为了解决这个问题,我需要在查询中加入一个正则表达式。所以我尝试了以下方法,但其中 none 有效:
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['$/.*java.*/']}}) #ignoring sql for now
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['sql',re.compile('$/.*java.*/', re.IGNORECASE)]}})
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['sql',re.compile('/.*java.*/', re.IGNORECASE)]}})
对我应该做什么有什么想法吗?
http://docs.mongodb.org/manual/reference/operator/query/regex/#syntax-restrictions
您的正则表达式不正确。试试这些。
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : [/.*java.*/i]}}) #ignoring sql for now
Job.objects(__raw__={'attributes.Skills.Skill' : {'$in' : ['sql',re.compile('.*java.*', re.IGNORECASE)]}})