java - Best way to load multiple objects by ObjectId in order in a single query? -


i have list of objectids objects want load mongodb. loop through each , using java driver, inefficient.

i tried morphia datastore.get(java.lang.class<t> tclass, java.lang.iterable<v> vs) method , passed list of ids. unfortunately order not preserved, guess because it's using $in: {...} syntax.

is there way single query , objects in arbitrary order?

update: clear, have ordered list of ids , want load corresponding objects in same order.

as far know, there not such option, , don't need such option.

what i've done using mongo create findbyids(iterable<objectid> ids) morphia seems do, except return result map<objectid,model> (or map<string,model>)

there different possible strategies handle ids couldn't found:

  • do not put key in map (which choose)
  • put key null value
  • raise exception

and iterated on iterable i'm sure preserves iteration order (using linkedhashmap)

you similar. have need sort mongodb result in iteration order. in memory, doesn't cost much...


Comments