2011-02-12 18 views
15

मैं एक regex कैसे निर्दिष्ट और मामले को अनदेखा कर सकते हैं:अजगर MongoDB regex: उपेक्षा मामले

regex = ".*" + filter + ".*"; 
config.gThingCollection.find({"name":{"$regex":regex}}) 

मैं केस-संवेदी होने के लिए फिल्टर चाहते हैं, कैसे है कि प्राप्त करने के लिए?

उत्तर

28

इसके बजाय पाइथन रेगेक्स ऑब्जेक्ट्स का उपयोग करने का प्रयास करें। Pymongo उन्हें सही ढंग से क्रमबद्ध करेगा:

import re 
config.gThingCollection.find({"name": re.compile(regex, re.IGNORECASE)}) 
17

आप अपनी क्वेरी में MongoDB regex विकल्पों का उपयोग कर सकते हैं।

config.gThingCollection.find({"name":{"$regex":regex, "$options": "-i"}}) 
1
res = posts.find({"geography": {"$regex": 'europe', "$options": 'i'}}) 

# if you need use $not 
import re 
res = posts.find(
    {"geography": {"$not": re.compile('europe'), "$options": 'i'}}) 
संबंधित मुद्दे