2013-08-01 45 views
3

我想在mongo + java中用$ regex寫查詢$。它在mongo shell中也不工作。我的意思是我沒有得到任何結果,但沒有查詢解析錯誤。 下面是最終查詢我從Java調試拿到行,其中我說collection.find(finalQuery) `

{ "$and" : [ 
     { "$or" : 
       [ { "country" : "united states"}]} , 
     { "businesses" : 
       { "$in" : [ { "$regex" : "^.*cardinal.*health.*$"} , { "$regex" :  "^.*the.*hartford.*$"}]} 
     } 
     ] 
} 

Java Code snipet for Above query: 集企業=新的HashSet();

for(String st: srchTerms){ 

businesses.add(Pattern.compile("^"+st.trim()+"$")); 

} 
srchTermQuery.append("businesses", new BasicDBObject("$in", businesses)); 

` 但是,下面的查詢在蒙戈外殼的作品,但我不知道如何把它寫成Java:

{"registering_organization" : {"$in":[/^.*cardinal.*health.*$/,/^.*the.*hartford.*$/]}}

的Java代碼添加各地正則表達式雙引號,如果我們試圖將其定義爲一個字符串。

+0

是的,我可以通過shell重現有關MongoDB 2.4.5這個問題。我建議在他們的JIRA網站上提交一個錯誤:https://jira.mongodb.org – gerrytan

+0

沒錯,我忘了提及版本。我也是mongodb 2.4.5。 – nir

回答

3

你看到的可能是一個錯誤的行爲,但是作爲替代,你可以寫你這樣的查詢

Pattern pattern = Pattern.compile("(^aaa$)|(^bbb$)"); 
srchTermQuery.append("businesses", pattern); 

不漂亮,但它似乎這樣的伎倆

+0

謝謝,gerry!沒想到那個!這將工作 – nir

+0

然而一個正則表達式與多個「|」似乎在查詢中表現比$差。我可以發送explain()感興趣。我也嘗試過$或者查詢,而不是$ in,這裏的表現也好於$( – nir

2

你不將能夠轉換:

{"businesses" : {"$in":[/^.*cardinal.*health.*$/,/^.*the.*hartford.*$/]}} 

直接轉換成Java正則表達式。這不是一個錯誤,這是因爲Java驅動程序在創建正則表達式查詢時使用$ regex格式以避免任何歧義。

$regex documentation指出

db.collection.find({ field: /acme.*corp/ }); 
db.collection.find({ field: { $regex: 'acme.*corp' } }); 

所以你的Java生成的查詢:

{ "businesses" : { "$in" : [ { "$regex" : "^.*cardinal.*health.*$"}, 
          { "$regex" : "^.*the.*hartford.*$"}]} 
       } 
} 

是完全等價的查詢你想轉換:

{"businesses" : {"$in": [/^.*cardinal.*health.*$/, 
         /^.*the.*hartford.*$/]} 
       } 
} 

在總結,你寫的Java已經是轉換查詢的正確方法anted。我已經在我自己的測試中運行了它,並且它返回了預期的結果。

也許如果你包含了一些你希望通過查詢返回的示例文檔,我們可以進一步提供幫助嗎?

+0

)。我仍然無法從$ in和regex組合中獲得結果。下面是我從類似查詢中獲得的示例數據集
'{「$ or」:[{「registering_organization」:{「$ regex」:「^。* cardinal。* health。* $」}},{「registering_organization」:{「$ regex」:「^。* the 。*哈特福德。* $「}}]}'sample data:
'{」country「:」united states「,」registering_organization「:」the hartford-070531ads308「}, {」country「:」united states「, 「registering_organization」:「thehartfordpayerw9809523465」}' – nir

+3

$ in操作符不支持$ regex,你可以在這裏看到:https://docs.mongodb.com/manual/reference/operator/query/in/#use-the -in-操作者上帶有一個正則表達式 – oblivion

1

我有一個需要列出所有以指定字符串開頭的鍵。在命令行下面的工作對我來說:

db.crawlHTML.count({"_id": /^1001/}) 

以下是Java實現:

public List<String> listKeysLike(DB mongoDB, String likeChars) throws Exception { 

    DBCollection dbCollection = this.getHTMLCollection(mongoDB, TESTPROD); 
    List<String> keyList = new ArrayList<String>(); 

    BasicDBObject query = new BasicDBObject();  
    String queryString = "^" + likeChars.trim() ; // setup regex 
    query.put("_id", java.util.regex.Pattern.compile(queryString)); 
    DBCursor cursor = dbCollection.find(query); 

    while (cursor.hasNext()) {  // _id used as the primary key 
     BasicDBObject obj = (BasicDBObject) cursor.next(); 
     String tempString = obj.getString("_id"); 
     keyList.add(tempString); 
    }  // while 

    return keyList; 
} 

注:在「TESTPROD」只是告訴我,我應該使用哪兩個數據庫。

0

你必須使用MongoDB的正則表達式的符號,而不是把它在一個字符串

db.somecollection.find({records: {$in: [/.*somestring.*/]}})