Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

In this page you can find the example usage for com.mongodb BasicDBList BasicDBList.

Prototype

BasicDBList

Source Link

Usage

From source file:com.groupon.jenkins.mongo.JenkinsEmbeddedMapper.java

License:Open Source License

@Deprecated
private void readCollection(final DBObject dbObject, final MappedField mf, final Object entity,
        final EntityCache cache, final Mapper mapper) {
    // multiple documents in a List
    final Collection values = mf.isSet() ? mapper.getOptions().getObjectFactory().createSet(mf)
            : mapper.getOptions().getObjectFactory().createList(mf);

    final Object dbVal = mf.getDbObjectValue(dbObject);
    if (dbVal != null) {

        final List dbValues;
        if (dbVal instanceof List) {
            dbValues = (List) dbVal;
        } else {//from ww w. j a v  a  2 s.  c om
            dbValues = new BasicDBList();
            dbValues.add(dbVal);
        }

        for (final Object o : dbValues) {

            Object newEntity = null;

            if (o != null) {
                //run converters
                if (mapper.getConverters().hasSimpleValueConverter(mf)
                        || mapper.getConverters().hasSimpleValueConverter(mf.getSubClass())) {
                    newEntity = mapper.getConverters().decode(mf.getSubClass(), o, mf);
                } else if (mapper.getConverters().hasSimpleValueConverter(o.getClass())) {// added this condition to handle incorrectly mapped primitives.
                    newEntity = mapper.getConverters().decode(o.getClass(), o, mf);
                } else {
                    if (o instanceof DBObject) {
                        newEntity = readMapOrCollectionOrEntity((DBObject) o, mf, cache, mapper);
                    } else {
                        throw new MappingException(
                                "Embedded element isn't a DBObject! How can it be that is a " + o.getClass());
                    }
                }
            }

            values.add(newEntity);
        }
    }
    if (!values.isEmpty() || mapper.getOptions().isStoreEmpties()) {
        if (mf.getType().isArray()) {
            mf.setFieldValue(entity,
                    ReflectionUtils.convertToArray(mf.getSubClass(), ReflectionUtils.iterToList(values)));
        } else {
            mf.setFieldValue(entity, values);
        }
    }
}

From source file:com.groupon.jenkins.mongo.ParametersDefinitionPropertyCoverter.java

License:Open Source License

@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null)
        return null;

    ParametersDefinitionProperty parametersDefinitionProperty = (ParametersDefinitionProperty) value;

    BasicDBList parameters = new BasicDBList();
    for (ParameterDefinition definition : parametersDefinitionProperty.getParameterDefinitions()) {
        parameters.add(getMapper().toDBObject(definition));
    }/*from   w  ww.  j av  a2 s .com*/

    return parameters;
}

From source file:com.ibm.db2j.MongoDB.java

License:Open Source License

/**
 * This method adds an operator to the mongo query to enforce the derby qualifiers specified.
 * @param query - Object representing the conditions of a mongoDB query. 
 *               This will be modified to enforce the qualRow conditions
 * @param qualRow - an array of derby database qualifiers, holding a set of conditions 
 *                   that should be added to the query. 
 * @param operator - indicates how the qualRow conditions should be combined with the query - "OR" or "AND".
 *//*from   w  ww .j a  v  a2 s .c o m*/
public void addMongoOperatorRow(BasicDBObject query, Qualifier qualRow[], CombinationOperator operator) {
    if (qualRow == null || qualRow.length == 0) {
        // Don't include any entry for this row.
    } else if (qualRow.length == 1) {
        // just include the single entry
        addMongoOperator(query, qualRow[0]);
    } else {
        // we have > 1 qualifier so they need including in a list, prefixed by the
        // necessary operator.
        String combinationText;
        switch (operator) {
        case OR:
            combinationText = "$or";
            break;
        case AND:
            combinationText = "$and";
            break;
        default: // throw exception
            combinationText = "$invalid";
        }

        // construct a separate sub-list of conditions to hold the qualifiers. 
        BasicDBList insideOperatorList = new BasicDBList();
        addMongoOperators(insideOperatorList, qualRow);
        query.append(combinationText, insideOperatorList);
    }
}

From source file:com.ibm.ws.lars.rest.PersistenceBean.java

License:Apache License

/**
 * Create a filter object for a mongodb query from a filtermap and search term
 *
 * @param filters the filter map// ww  w.j  a v  a  2 s .  co  m
 * @param searchTerm the search term
 * @return a filter object which can be passed as a query to mongodb find()
 */
private BasicDBObject createFilterObject(Map<String, List<Condition>> filters, String searchTerm) {

    // Must return an empty object if there are no filters or search term
    if ((filters == null || filters.isEmpty()) && searchTerm == null) {
        return new BasicDBObject();
    }

    // Need to use a filterList and an $and operator because we may add multiple $or sections
    // which would overwrite each other if we just appended them to the filterObject
    BasicDBList filterList = new BasicDBList();
    BasicDBObject filterObject = new BasicDBObject("$and", filterList);

    for (Entry<String, List<Condition>> filter : filters.entrySet()) {
        List<Condition> conditions = filter.getValue();
        if (conditions.size() == 1) {
            filterList.add(createFilterObject(filter.getKey(), conditions.get(0)));
        } else {
            BasicDBList list = new BasicDBList();
            for (Condition condition : conditions) {
                list.add(createFilterObject(filter.getKey(), condition));
            }
            filterList.add(new BasicDBObject("$or", list));
        }
    }

    if (searchTerm != null) {
        BasicDBObject value = new BasicDBObject("$search", searchTerm);
        BasicDBObject searchObject = new BasicDBObject("$text", value);
        filterList.add(searchObject);
    }

    return filterObject;
}

From source file:com.icoin.trading.tradeengine.query.tradeexecuted.repositories.TradeExecutedQueryRepositoryImpl.java

License:Apache License

@Override
public List<OpenHighLowCloseVolume> ohlc(String orderBookIdentifier, Date start, Date end, Pageable pageable) {
    hasLength(orderBookIdentifier);//from   w w w. j a  v  a  2 s  .c  om
    notNull(pageable);

    DBObject command = BasicDBObjectBuilder.start("aggregate", TRADE_EXECUTED_ENTRY_COLLECTION).get();

    final DBObject criteriaObject = Criteria.where("orderBookIdentifier").is(orderBookIdentifier)
            .and("tradeTime").gte(start).lt(end).getCriteriaObject();
    DBObject match = BasicDBObjectBuilder.start("$match", criteriaObject).get();

    DBObject projection = BasicDBObjectBuilder
            .start("year", BasicDBObjectBuilder.start("$year", "$tradeTime").get())
            .append("month", BasicDBObjectBuilder.start("$month", "$tradeTime").get())
            .append("day", BasicDBObjectBuilder.start("$dayOfMonth", "$tradeTime").get())
            .append("hour", BasicDBObjectBuilder.start("$hour", "$tradeTime").get())
            .append("minute", BasicDBObjectBuilder.start("$minute", "$tradeTime").get())
            .append("tradedPrice", 1).append("tradedAmount", 1).append("_id", 0).get();

    DBObject project = BasicDBObjectBuilder.start("$project", projection).get();

    //{ "aggregate" : "tradeExecutedEntry" ,
    // "pipeline" : [ { "$match" : { "orderBookIdentifier" : "f830f7e3-9f99-4688-92e7-6dbafc7220a8" ,
    // "tradeTime" : { "$gte" : { "$date" : "2007-12-12T04:12:12.120Z"} ,
    // "$lt" : { "$date" : "2012-12-12T04:12:04.120Z"}}}} ,
    // { "$project" : { "tradeTime" : 1 , "tradedPrice.amount" : 1 , "tradedAmount.amount" : 1 , "year" :
    // { "$year" : [ "$tradeTime"]} , "month" : { "$month" : [ "$tradeTime"]} , "week" : { "$week" : [ "$tradeTime"]}}} ,
    // { "$group" : { "_id" : "$year" , "open" : { "$first" : "$tradedPrice"} , "high" : { "$max" : "$tradedPrice"} ,
    // "low" : { "$min" : "$tradedPrice"} , "close" : { "$last" : "$tradedPrice"} , "volume" : { "$sum" : "$tradedAmount"}}} ,
    // { "$skip" : 0} , { "$limit" : 100}]}

    //        {"$project": {
    //            "year":       {"$year": "$dt"},
    //            "month":      {"$month": "$dt"},
    //            "day":        {"$dayOfMonth": "$dt"},
    //            "hour":       {"$hour": "$dt"},
    //            "minute":     {"$minute": "$dt"},
    //            "second":     {"$second": "$dt"},
    //            "dt": 1,
    //                    "p": 1 }},
    //        {"_id" : {"year": "$year", "month": "$month", "day": "$day", "hour": "$hour", "minute": "$minute" },
    //            "open":  {"$first": "$p"},
    //            "high":  {"$max": "$p"},
    //            "low":   {"$min": "$p"},
    //            "close": {"$last": "$p"} }} ] )

    //        02:41:22.649 [main] DEBUG c.i.t.t.q.t.r.TradeExecutedQueryRepositoryImpl - aggregation { "aggregate" : "tradeExecutedEntry" , "pipeline" : [ { "$match" : { "orderBookIdentifier" : "c623022b-9baa-437a-a70f-b59adead3ecf" , "tradeTime" : { "$gte" : { "$date" : "2007-12-12T04:12:12.120Z"} , "$lt" : { "$date" : "2012-12-12T04:12:04.120Z"}}}} , { "$project" : { "year" : { "$year" : "$tradeTime"} , "month" : { "$month" : "$tradeTime"} , "day" : { "$dayOfMonth" : "$tradeTime"} , "hour" : { "$hour" : "$tradeTime"} , "minute" : { "$minute" : "$tradeTime"} , "tradedPrice" : 1 , "tradedAmount" : 1 , "_id" : 0}} , { "$group" : { "_id" : { "year" : "$year" , "priceCcy" : "$tradedPrice.currency" , "amountCcy" : "$tradedAmount.currency"} , "open" : { "$first" : "$tradedPrice.amount"} , "high" : { "$max" : "$tradedPrice.amount"} , "low" : { "$min" : "$tradedPrice.amount"} , "close" : { "$last" : "$tradedPrice.amount"} , "volume" : { "$sum" : "$tradedAmount.amount"}}}]} found :[ { "_id" : { "year" : 2012 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 11550000000} , { "_id" : { "year" : 2010 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 2100000000} , { "_id" : { "year" : 2011 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 1050000000}]
    //        02:46:45.023 [main] DEBUG c.i.t.t.q.t.r.TradeExecutedQueryRepositoryImpl - aggregation { "aggregate" : "tradeExecutedEntry" , "pipeline" : [ { "$match" : { "orderBookIdentifier" : "04527652-b53b-47bf-967d-2001fbe18c13" , "tradeTime" : { "$gte" : { "$date" : "2007-12-12T04:12:12.120Z"} , "$lt" : { "$date" : "2012-12-12T04:12:04.120Z"}}}} , { "$project" : { "year" : { "$year" : "$tradeTime"} , "month" : { "$month" : "$tradeTime"} , "day" : { "$dayOfMonth" : "$tradeTime"} , "hour" : { "$hour" : "$tradeTime"} , "minute" : { "$minute" : "$tradeTime"} , "tradedPrice" : 1 , "tradedAmount" : 1 , "_id" : 0}} , { "$group" : { "_id" : { "year" : "$year" , "priceCcy" : "$tradedPrice.currency" , "amountCcy" : "$tradedAmount.currency"} , "open" : { "$first" : "$tradedPrice.amount"} , "high" : { "$max" : "$tradedPrice.amount"} , "low" : { "$min" : "$tradedPrice.amount"} , "close" : { "$last" : "$tradedPrice.amount"} , "volume" : { "$sum" : "$tradedAmount.amount"}}}]} found :[ { "_id" : { "year" : 2012 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 11550000000} , { "_id" : { "year" : 2010 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 2100000000} , { "_id" : { "year" : 2011 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 1050000000}]

    final DBObject groupId = BasicDBObjectBuilder.start("year", "$year")
            //                .append("month", "$month")
            //                .append("day", "$dayOfMonth")
            //                .append("hour", "$hour")
            //                .append("minute", "$minute")
            .append("priceCcy", "$tradedPrice.currency").append("amountCcy", "$tradedAmount.currency").get();
    DBObject groupOp = BasicDBObjectBuilder.start("_id", groupId)
            .append("open", BasicDBObjectBuilder.start("$first", "$tradedPrice.amount").get())
            .append("high", BasicDBObjectBuilder.start("$max", "$tradedPrice.amount").get())
            .append("low", BasicDBObjectBuilder.start("$min", "$tradedPrice.amount").get())
            .append("close", BasicDBObjectBuilder.start("$last", "$tradedPrice.amount").get())
            .append("volume", BasicDBObjectBuilder.start("$sum", "$tradedAmount.amount").get()).get();

    DBObject group = BasicDBObjectBuilder.start("$group", groupOp).get();

    final BasicDBList pipeline = new BasicDBList();
    pipeline.add(match);
    pipeline.add(project);
    pipeline.add(group);
    command.put("pipeline", pipeline);

    CommandResult commandResult = mongoTemplate.executeCommand(command);
    handleCommandError(commandResult, command);

    // map results
    @SuppressWarnings("unchecked")
    Iterable<DBObject> resultSet = (Iterable<DBObject>) commandResult.get("result");
    List<OpenHighLowCloseVolume> mappedResults = Lists.newLinkedList();

    if (logger.isDebugEnabled()) {
        logger.debug("aggregation {} found :{}", command, resultSet);
    }

    System.err.println(Long.MAX_VALUE / 100000000);
    return null;
}

From source file:com.ifactory.service.weather.photo.PhotoService.java

License:Apache License

public boolean add(Photo photo) {
    DB db = mongoClient.getDB(this.dbName);
    DBCollection coll = db.getCollection(PHOTO_COLLECTION);

    BasicDBList geo = new BasicDBList();
    geo.add(photo.getLatitude());/*from   w  w w. j av  a  2s  .  c om*/
    geo.add(photo.getLongitude());

    BasicDBObject query = new BasicDBObject("name", photo.getName()).append("day", photo.getDay())
            .append("weather", photo.getWeatherId()).append("timestamp", photo.getTimestamp())
            .append("geo", new BasicDBObject("type", "Point").append("coordinates", geo));

    try {
        coll.insert(query);
    } catch (MongoException e) {
        return false;
    }

    return true;
}

From source file:com.ifactory.service.weather.photo.PhotoService.java

License:Apache License

private BasicDBObject setGeoCoord(double lat, double lng, double radius) {
    BasicDBObject query = new BasicDBObject();
    BasicDBList geo = new BasicDBList();
    geo.add(lat);//from w  ww. j a va 2  s. co m
    geo.add(lng);
    BasicDBList center = new BasicDBList();
    center.add(geo);
    center.add(radius);
    query.append("geo.coordinates", new BasicDBObject("$within", new BasicDBObject("$center", center)));
    return query;
}

From source file:com.ikanow.aleph2.security.web.IkanowV1CookieAuthentication.java

License:Apache License

protected String lookupProfileIdByEmail(String email) {
    String profileId = null;// w  ww. j a va  2s.co m
    try {
        DBObject clause1 = new BasicDBObject("username", email);
        DBObject clause2 = new BasicDBObject("WPUserID", email);
        BasicDBList or = new BasicDBList();
        or.add(clause1);
        or.add(clause2);
        DBObject query = new BasicDBObject("$or", or);
        DBObject result = getAuthenticationStore().findOne(query);
        profileId = result != null ? "" + result.get("profileId") : null;
    } catch (Exception e) {
        logger.error("lookupProfileIdByEmail caught exception", e);
    }
    return profileId;
}

From source file:com.ikanow.aleph2.shared.crud.mongodb.utils.MongoDbUtils.java

License:Apache License

/** Creates a big $and/$or list of the list of fields in the single query component
 * @param andVsOr - top level MongoDB operator
 * @param query_in - a single query (ie set of fields)
 * @return the MongoDB query object (no meta - that is added above)
 *///  ww w .  ja  va 2s.  co  m
protected static <T> DBObject convertToMongoQuery_single(final String andVsOr,
        final SingleQueryComponent<T> query_in) {
    final LinkedHashMultimap<String, Tuple2<Operator, Tuple2<Object, Object>>> fields = query_in.getAll();

    // The actual query:

    return Patterns.match(fields).<DBObject>andReturn().when(f -> f.isEmpty(), f -> new BasicDBObject())
            .otherwise(f -> f.asMap().entrySet().stream()
                    .<Tuple2<String, Tuple2<Operator, Tuple2<Object, Object>>>>flatMap(
                            entry -> entry.getValue().stream()
                                    .map(val -> Tuples._2T(entry.getKey(),
                                            Tuples._2T(val._1(), removeEnums(val._2())))))
                    .collect(Collector.of(BasicDBObject::new, (acc, entry) -> {
                        Patterns.match(acc.get(andVsOr)).andAct().when(l -> (null == l), l -> {
                            BasicDBList dbl = new BasicDBList();
                            dbl.add(operatorToMongoKey(entry._1(), entry._2()));
                            acc.put(andVsOr, dbl);
                        }).when(BasicDBList.class, l -> l.add(operatorToMongoKey(entry._1(), entry._2())))
                                .otherwise(() -> {
                                });
                    }, (a, b) -> {
                        a.putAll(b.toMap());
                        return a;
                    }, Characteristics.UNORDERED)));
}

From source file:com.ikanow.infinit.e.api.knowledge.federated.SimpleFederatedQueryEngine.java

License:Open Source License

@Override
public void postQueryActivities(ObjectId queryId, List<BasicDBObject> docs, ResponsePojo response) {
    boolean grabbedScores = false;
    double aggregateSignif = 100.0;
    double queryRelevance = 100.0;
    double score = 100.0;

    if (null != _asyncRequestsPerQuery) {
        int added = 0;
        BasicDBList bsonArray = new BasicDBList();
        PeekingIterator<FederatedRequest> it = Iterators.peekingIterator(_asyncRequestsPerQuery.iterator());
        while (it.hasNext()) {
            // loop state:
            BasicDBObject[] docOrDocs = new BasicDBObject[1];
            docOrDocs[0] = null;/*from   w w  w.  j av a 2s  . c o  m*/

            FederatedRequest request = it.next();
            boolean isComplexSource = isComplexSource(request.endpointInfo.parentSource);
            if (null == request.cachedDoc) { // no cached doc, simple source processing (OR ANY COMPLEX CASE BY CONSTRUCTION)
                try {
                    if ((null == request.cachedResult) || isComplexSource) { // no cached api response, or complex         
                        if (null != request.importThread) {
                            // 1) wait for the thread to finish
                            if (null == request.endpointInfo.queryTimeout_secs) {
                                request.endpointInfo.queryTimeout_secs = 300;
                            }
                            for (int timer = 0; timer < request.endpointInfo.queryTimeout_secs; timer++) {
                                try {
                                    request.importThread.join(1000L);
                                    if (!request.importThread.isAlive()) {
                                        break;
                                    }
                                } //TESTED (by hand)
                                catch (Exception e) {
                                    //(carry on)
                                }
                            }
                            if (request.importThread.isAlive()) {
                                request.errorMessage = new RuntimeException("Script timed out");
                            } //TESTED (by hand)

                            // 2) Get the results
                            if (null != request.errorMessage) {
                                if (_testMode) {
                                    throw new RuntimeException(request.errorMessage);
                                }
                            } else if (isComplexSource) {
                                //DEBUG 
                                if (_DEBUG)
                                    _logger.debug("DEB: postQA0: " + request.complexSourceProcResults.size());

                                handleComplexDocCaching(request, _cacheMode, _scoreStats);

                                // Get a list of docs
                                docOrDocs = ((BasicDBList) DocumentPojo
                                        .listToDb(request.complexSourceProcResults, DocumentPojo.listType()))
                                                .toArray(new BasicDBObject[0]);

                                // (_API_ caching is exactly the same between cache and non-cache cases)
                                // (note that if null != complexSourceProcResults then follows that null != scriptResult)
                                String url = buildScriptUrl(request.mergeKey, request.queryIndex);

                                if (!(request.importThread instanceof FederatedSimpleHarvest) && _cacheMode) { // (don't cache python federated queries in test mode)
                                    // (simple harvest caching is done separately)
                                    this.cacheApiResponse(url, request.scriptResult, request.endpointInfo);
                                }
                            } //TESTED (by hand - single and multiple doc mode)               
                            else if (null == request.scriptResult) {
                                if (_testMode) {
                                    throw new RuntimeException("Script mode: no cached result found from: "
                                            + request.requestParameter);
                                }
                            } else {
                                // (_API_ caching is exactly the same between cache and non-cache cases)
                                String url = buildScriptUrl(request.mergeKey, request.queryIndex);
                                if (_cacheMode) { // (don't cache python federated queries in test mode)
                                    this.cacheApiResponse(url, request.scriptResult, request.endpointInfo);
                                }
                                bsonArray.add(request.scriptResult);
                            }
                        } // end script mode
                        else { // HTTP mode (also: must be simple source builder)
                            Response endpointResponse = request.responseFuture.get();
                            request.asyncClient.close();
                            request.asyncClient = null;

                            String jsonStr = endpointResponse.getResponseBody();
                            String url = endpointResponse.getUri().toURL().toString();

                            Object bsonUnknownType = com.mongodb.util.JSON.parse(jsonStr);
                            BasicDBObject bson = null;
                            if (bsonUnknownType instanceof BasicDBObject) {
                                bson = (BasicDBObject) bsonUnknownType;
                            } else if (bsonUnknownType instanceof BasicDBList) {
                                bson = new BasicDBObject(SimpleFederatedCache.array_, bsonUnknownType);
                            } else if (bsonUnknownType instanceof String) {
                                bson = new BasicDBObject(SimpleFederatedCache.value_, bsonUnknownType);
                            }

                            //DEBUG
                            if (_DEBUG)
                                _logger.debug("DEB: postQA1: " + url + ": " + jsonStr);

                            if (null != bson) {
                                MongoDbUtil.enforceTypeNamingPolicy(bson, 0);
                                this.cacheApiResponse(url, bson, request.endpointInfo);
                                bsonArray.add(bson);
                            }
                        } //(end script vs request method)
                    } //TESTED (3.1, 4.2)
                    else { // (just used cached value)
                        //DEBUG 
                        if (_DEBUG)
                            _logger.debug("DEB: postQA2: " + request.cachedResult.toString());

                        bsonArray.add(
                                (BasicDBObject) request.cachedResult.get(SimpleFederatedCache.cachedJson_));
                    } //TESTED (4.1, 4.3)
                } catch (Exception e) {
                    //DEBUG
                    if (null == request.subRequest) {
                        _logger.error("Error with script: " + e.getMessage());
                        if (_testMode) {
                            throw new RuntimeException("Error with script: " + e.getMessage(), e);
                        }
                    } else {
                        _logger.error("Error with " + request.subRequest.endPointUrl + ": " + e.getMessage());
                        if (_testMode) {
                            throw new RuntimeException(
                                    "Error with " + request.subRequest.endPointUrl + ": " + e.getMessage(), e);
                        }
                    }
                }

                if (null == docOrDocs[0]) {
                    // (this next bit of logic can only occur in simple source cases by construction, phew)
                    if (!it.hasNext() || (request.mergeKey != it.peek().mergeKey)) { // deliberate ptr arithmetic
                        String url = buildScriptUrl(request.mergeKey, request.queryIndex);

                        //DEBUG
                        if (_DEBUG)
                            _logger.debug("DEB: postQA3: " + url + ": " + bsonArray);

                        docOrDocs[0] = createDocFromJson(bsonArray, url, request, request.endpointInfo);
                    }
                }
            } // (end if no cached doc)
            else { // cached doc, bypass lots of processing because no merging and doc already built (simple source processing)
                docOrDocs[0] = request.cachedDoc;
            } //TESTED (by hand)

            if (null != docOrDocs[0])
                for (BasicDBObject doc : docOrDocs) {

                    // Cache the document unless already cached (or caching disabled)
                    if ((null == request.cachedDoc) && _cacheMode && !isComplexSource
                            && ((null == request.endpointInfo.cacheTime_days)
                                    || (request.endpointInfo.cacheTime_days >= 0))) {
                        simpleDocCache(request, doc);
                    } //TESTED (by hand, 3 cases: cached not expired, cached expired first time, cached expired multiple times)

                    if (!grabbedScores) {
                        if (!docs.isEmpty()) {
                            BasicDBObject topDoc = docs.get(0);
                            aggregateSignif = topDoc.getDouble(DocumentPojo.aggregateSignif_, aggregateSignif);
                            queryRelevance = topDoc.getDouble(DocumentPojo.queryRelevance_, queryRelevance);
                            score = topDoc.getDouble(DocumentPojo.score_, score);
                            grabbedScores = true;

                            // OK would also like to grab the original matching entity, if it exists
                            if (!isComplexSource) {
                                BasicDBList ents = (BasicDBList) topDoc.get(DocumentPojo.entities_);
                                if (null != ents) {
                                    for (Object entObj : ents) {
                                        BasicDBObject ent = (BasicDBObject) entObj;
                                        String entIndex = ent.getString(EntityPojo.index_, "");
                                        if (entIndex.equals(request.queryIndex)) {
                                            ents = (BasicDBList) doc.get(DocumentPojo.entities_);
                                            if (null != ents) {
                                                ents.add(ent);
                                            }
                                            break;
                                        }
                                    }
                                } //TESTED (by hand)
                            }
                        }
                    }
                    doc.put(DocumentPojo.aggregateSignif_, aggregateSignif);
                    doc.put(DocumentPojo.queryRelevance_, queryRelevance);
                    doc.put(DocumentPojo.score_, score);

                    // Swap id and updateId, everything's been cached now:
                    // Handle update ids vs normal ids:
                    ObjectId updateId = (ObjectId) doc.get(DocumentPojo.updateId_);
                    if (null != updateId) { // swap the 2...
                        doc.put(DocumentPojo.updateId_, doc.get(DocumentPojo._id_));
                        doc.put(DocumentPojo._id_, updateId);
                    } //TESTED (by hand)            

                    // If we're returning to a query then we'll adjust the doc format (some of the atomic fields become arrays)
                    if (!_testMode) {
                        convertDocToQueryFormat(doc, request.communityIdStrs);
                    } //TESTED (by hand)

                    docs.add(0, doc);
                    added++;
                    //(doc auto reset at top of loop)

                    //(end if built a doc from the last request/set of requests)
                } //TESTED (3.1)      

        } //(end loop over federated requests)

        if (null != response.getStats()) {
            response.getStats().found += added;
        } //TESTED (by hand)         
    }
}