2

I'm using jongo API - org.jongo.MongoCollection is the class.

I have list of object ids and converted the same as ObjectId[] and trying to query as follows

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

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

The query doesn't work as specified in the URL In Jongo, how to find multiple documents from Mongodb by a list of IDs

Any suggestion on how to resolve?

Thanks.

Community
  • 1
  • 1
Kathiresa
  • 421
  • 2
  • 6
  • 15

1 Answers1

3

Try it with a List as shown on the docs:

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

For example the following snippet I crafted quick & dirty right now worked for me (I use older verbose syntax as I am currently on such a system ...)

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();
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • This doesn't work as collection.find returns MongoCursor and throwing cast exception. however the following works fine. How to covert MongoCursor to user defined List. i'm using the following List collectionsAsList = new ArrayList(); MongoCursor cursorCollection = collection.find("{ _id: { $in: # } }", ids).as(Friends.class); while (cursorCollection.hasNext()) { collectionsAsList.add(cursorCollection.next()); } – Kathiresa Aug 29 '16 at 09:56
  • It does work but you have to have a suitable `Friend` class of course - but that was also not the topic of your question but it was the way how to pass the list of IDs and that's the question if that works for you or not – DAXaholic Aug 29 '16 at 09:59
  • That's worked fine and thanks a lot. I had the suitable class but does it type cast properly as it currently throws compilation error. is it possible to put a code snippet here? – Kathiresa Aug 29 '16 at 17:43