DBObject createIdQuery(K object) { return new BasicDBObject("_id", convertToDbId(object)); }
DBObject createIdQuery(K object) { return new BasicDBObject("_id", convertToDbId(object)); }
/** * Fetch a collection of dbrefs. This is more efficient than fetching one at a time. * * @param collection the collection to fetch * @param fields The fields to retrieve for each of the documents * @return The collection of referenced objcets */ public <R, RK> List<R> fetch(Collection<DBRef<R, RK>> collection, DBObject fields) { Map<JacksonCollectionKey, List<Object>> collectionsToIds = new HashMap<JacksonCollectionKey, List<Object>>(); for (DBRef<R, RK> ref : collection) { if (ref instanceof FetchableDBRef) { JacksonCollectionKey key = ((FetchableDBRef) ref).getCollectionKey(); List<Object> ids = collectionsToIds.get(key); if (ids == null) { ids = new ArrayList<Object>(); collectionsToIds.put(key, ids); } ids.add(getReferenceCollection(key).convertToDbId(ref.getId())); } } List<R> results = new ArrayList<R>(); for (Map.Entry<JacksonCollectionKey, List<Object>> entry : collectionsToIds.entrySet()) { for (R result : this.<R, RK>getReferenceCollection(entry.getKey()).find( new QueryBuilder().put("_id").in(entry.getValue()).get(), fields)) { results.add(result); } } return results; }
/** * Fetch a collection of dbrefs. This is more efficient than fetching one at a time. * * @param collection the collection to fetch * @param fields The fields to retrieve for each of the documents * @return The collection of referenced objcets */ public <R, RK> List<R> fetch(Collection<DBRef<R, RK>> collection, DBObject fields) { Map<JacksonCollectionKey, List<Object>> collectionsToIds = new HashMap<JacksonCollectionKey, List<Object>>(); for (DBRef<R, RK> ref : collection) { if (ref instanceof FetchableDBRef) { JacksonCollectionKey key = ((FetchableDBRef) ref).getCollectionKey(); List<Object> ids = collectionsToIds.get(key); if (ids == null) { ids = new ArrayList<Object>(); collectionsToIds.put(key, ids); } ids.add(getReferenceCollection(key).convertToDbId(ref.getId())); } } List<R> results = new ArrayList<R>(); for (Map.Entry<JacksonCollectionKey, List<Object>> entry : collectionsToIds.entrySet()) { for (R result : this.<R, RK>getReferenceCollection(entry.getKey()).find( new QueryBuilder().put("_id").in(entry.getValue()).get(), fields)) { results.add(result); } } return results; }