@Path("bars") @GET @Produces(MediaType.APPLICATION_JSON) public List<Bar> listBars() { return getJacksonDBCollection().find().toArray(); }
@GET @Timed public List<Blog> blogsTimeline() { DBCursor<Blog> dbCursor = collection.find(); List<Blog> blogs = new ArrayList<>(); while (dbCursor.hasNext()) { Blog blog = dbCursor.next(); blogs.add(blog); } return blogs; }
@GET @Produces("text/html;charset=UTF-8") @Timed public View index() { DBCursor<Blog> dbCursor = collection.find(); List<Blog> blogs = new ArrayList<>(); while (dbCursor.hasNext()) { Blog blog = dbCursor.next(); blogs.add(blog); } return new IndexView(blogs); }
/** * Returns a single object from this collection matching the query. * * @param query the query object * @param fields an object for which every non null field will be returned * @param readPref The read preferences * @return the object found, or <code>null</code> if no such object exists */ public T findOne(T query, T fields, ReadPreference readPref) { DBCursor<T> cursor = find(query, fields).setReadPreference(readPref); if (cursor.hasNext()) { return cursor.next(); } else { return null; } }
/** * 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; }
/** * Returns a single object from this collection matching the query. * * @param query the query object * @param fields an object for which every non null field will be returned * @param readPref The read preferences * @return the object found, or <code>null</code> if no such object exists */ public T findOne(T query, T fields, ReadPreference readPref) { DBCursor<T> cursor = find(query, fields).setReadPreference(readPref); if (cursor.hasNext()) { return cursor.next(); } else { return null; } }
/** * Returns a single object from this collection matching the query. * * @param query the query object * @param fields fields to return * @param readPref The read preference * @return the object found, or <code>null</code> if no such object exists */ public T findOne(DBObject query, DBObject fields, ReadPreference readPref) { DBCursor<T> cursor = find(query, fields).setReadPreference(readPref); if (cursor.hasNext()) { return cursor.next(); } else { return null; } }
/** * Returns a single object from this collection matching the query. * * @param query the query object * @param fields fields to return * @param readPref The read preference * @return the object found, or <code>null</code> if no such object exists */ public T findOne(DBObject query, DBObject fields, ReadPreference readPref) { DBCursor<T> cursor = find(query, fields).setReadPreference(readPref); if (cursor.hasNext()) { return cursor.next(); } else { return null; } }
MapReduceOutput(JacksonDBCollection<?, ?> sourceCollection, com.mongodb.MapReduceOutput output, Class<T> type, Class<K> keyType) { this.output = output; if (output.getOutputCollection() != null) { this.outputCollection = JacksonDBCollection.wrap(output.getOutputCollection(), type, keyType, sourceCollection.getObjectMapper()); this.resultSet = outputCollection.find(); } else { this.outputCollection = null; Collection<T> results = new ArrayList<T>(); for (DBObject result : output.results()) { results.add(sourceCollection.convertFromDbObject(result, type)); } this.resultSet = 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; }
MapReduceOutput(JacksonDBCollection<?, ?> sourceCollection, com.mongodb.MapReduceOutput output, Class<T> type, Class<K> keyType) { this.output = output; if (output.getOutputCollection() != null) { this.outputCollection = JacksonDBCollection.wrap(output.getOutputCollection(), type, keyType, sourceCollection.getObjectMapper()); this.resultSet = outputCollection.find(); } else { this.outputCollection = null; Collection<T> results = new ArrayList<T>(); for (DBObject result : output.results()) { results.add(sourceCollection.convertFromDbObject(result, type)); } this.resultSet = results; } }