2016-08-23 30 views
2

我正在使用jongo API - org.jongo.MongoCollection是該類。

我有對象ID列表和轉換一樣ObjectId[],並試圖查詢作爲URL中指定In Jongo, how to find multiple documents from Mongodb by a list of IDs

如何任何建議如下

collection.find("{_id:{$in:#}}", ids).as(Employee.class); 

The query throws the exception - "java.lang.IllegalArgumentException: Too  
many parameters passed to query: {"_id":{"$in":#}}" 

查詢不起作用解決?

謝謝。

回答

3

List嘗試它的docs如圖所示:

List<String> ages = Lists.newArrayList(22, 63); 
friends.find("{age: {$in:#}}", ages); //→ will produce {age: {$in:[22,63]}} 

例如下面的片段我製作的快速&髒,現在工作對我來說(我用舊的冗長的語法我目前這樣的系統...)

List<ObjectId> ids = new ArrayList<ObjectId>(); 
ids.add(new ObjectId("57bc7ec7b8283b457ae4ef01")); 
ids.add(new ObjectId("57bc7ec7b8283b457ae4ef02")); 
ids.add(new ObjectId("57bc7ec7b8283b457ae4ef03")); 
int count = friends.find("{ _id: { $in: # } }", ids).as(Friend.class).count(); 
+0

這不起作用,因爲collection.find返回MongoCursor並拋出轉換異常。但以下工作正常。如何將MongoCursor隱藏到用戶定義的列表中。我使用以下列表 collectionsAsList = new ArrayList (); cursorCollection = collection.find(「{_id:{$ in:#}}」,ids).as(Friends.class); (cursorCollection.hasNext()){ collectionsAsList.add(cursorCollection.next()); } – Kathiresa

+0

它確實有效,但你必須有一個合適的「朋友」課程 - 但這也不是你的問題的主題,但它是如何傳遞ID列表的方式,這就是問題是否適用於你是否 – DAXaholic

+0

這工作得很好,非常感謝。我有合適的類,但是它正確地轉換類型,因爲它目前拋出編譯錯誤。是否可以在這裏放置一個代碼片段? – Kathiresa