/** * This method provides a static method to convert a DBObject into a given class. If the ObjectMapper is null, use a * default ObjectMapper * * @param dbObject * @param clazz * @param objectMapper * @return * @throws MongoException */ public static <S> S convertFromDbObject(DBObject dbObject, Class<S> clazz, ObjectMapper objectMapper) throws MongoException { return convertFromDbObject(dbObject, clazz, objectMapper, null); }
/** * Convert an array of DBObjects into the type for this collection, using the * Jackson ObjectMapper for this collection. * * @param dbObjects * @return * @throws MongoException */ public List<T> convertFromDbObjects(DBObject... dbObjects) throws MongoException { final List<T> results = new ArrayList<T>(dbObjects.length); for (DBObject dbObject : dbObjects) { results.add(convertFromDbObject(dbObject)); } return results; }
/** * returns an iterator to the results of the aggregation converted * back to the type of the generic using the Jackson ObjectMapper * from the JacksonDBCollection from which this AggregationResult * was created. * * @return A List Containing the result of the aggregation. */ public List<T> results() { // only generate our results list once if (results == null) { results = new ArrayList<T>(); Iterable<DBObject> iterable = output.results(); Iterator<DBObject> iter = iterable.iterator(); while (iter.hasNext()) { results.add((T) collection.convertFromDbObject(iter.next(), resultType)); } } return results; } }
/** * Converts this cursor to an array. * * @param max * the maximum number of objects to return * @return an array of objects * @throws MongoException * If an error occurred */ public List<T> toArray(int max) throws MongoException { executed(); if (max > all.size()) { List<DBObject> objects = cursor.toArray(max); for (int i = all.size(); i < objects.size(); i++) { all.add(jacksonDBCollection.convertFromDbObject(objects.get(i))); } } return all; }
/** * Returns the object the cursor is at and moves the cursor ahead by one. * * @return the next element * @throws MongoException */ @Override public T next() throws MongoException { executed(); current = jacksonDBCollection.convertFromDbObject(cursor.next()); return current; }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param remove * if true, document found will be removed * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(DBObject query, DBObject fields, DBObject sort, boolean remove, DBObject update, boolean returnNew, boolean upsert) { return convertFromDbObject(dbCollection.findAndModify( serializeFields(query), fields, sort, remove, update, returnNew, upsert)); }
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; } }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param remove * if true, document found will be removed * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(DBObject query, DBObject fields, DBObject sort, boolean remove, T update, boolean returnNew, boolean upsert) { return convertFromDbObject(dbCollection.findAndModify( serializeFields(query), fields, sort, remove, convertToBasicDbObject(update), returnNew, upsert)); }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param remove * if true, document found will be removed * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(DBObject query, DBObject fields, DBObject sort, boolean remove, DBUpdate.Builder update, boolean returnNew, boolean upsert) { return convertFromDbObject(dbCollection.findAndModify( serializeFields(query), fields, sort, remove, update.serialiseAndGet(objectMapper, type), returnNew, upsert)); }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param remove * if true, document found will be removed * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(DBQuery.Query query, DBObject fields, DBObject sort, boolean remove, DBUpdate.Builder update, boolean returnNew, boolean upsert) { return convertFromDbObject(dbCollection.findAndModify( serializeQuery(query), fields, sort, remove, update.serialiseAndGet(objectMapper, type), returnNew, upsert)); }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param remove * if true, document found will be removed * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(DBQuery.Query query, DBObject fields, DBObject sort, boolean remove, T update, boolean returnNew, boolean upsert) { return convertFromDbObject(dbCollection.findAndModify( serializeQuery(query), fields, sort, remove, convertToBasicDbObject(update), returnNew, upsert)); }