bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string ns = dbname + "." + cmdObj.firstElement().valuestr(); NamespaceDetails *d = nsdetails(ns); if (NULL == d) { errmsg = "can't find ns"; return false; } GeoNearArguments commonArgs(cmdObj); if (commonArgs.numWanted < 0) { errmsg = "numWanted must be >= 0"; return false; } vector<int> idxs; d->findIndexByType(IndexNames::GEO_2D, idxs); if (idxs.size() > 1) { errmsg = "more than one 2d index, not sure which to run geoNear on"; return false; } unordered_map<string, double> statsMap; if (1 == idxs.size()) { result.append("ns", ns); twod_internal::TwoDGeoNearRunner::run2DGeoNear(d, idxs[0], cmdObj, commonArgs, errmsg, result, &statsMap); BSONObjBuilder stats(result.subobjStart("stats")); for (unordered_map<string, double>::const_iterator it = statsMap.begin(); it != statsMap.end(); ++it) { stats.append(it->first, it->second); } stats.append("time", cc().curop()->elapsedMillis()); stats.done(); return true; } d->findIndexByType(IndexNames::GEO_2DSPHERE, idxs); if (idxs.size() > 1) { errmsg = "more than one 2dsphere index, not sure which to run geoNear on"; return false; } if (1 == idxs.size()) { result.append("ns", ns); run2DSphereGeoNear(d, idxs[0], cmdObj, commonArgs, errmsg, result); return true; } errmsg = "no geo indices for geoNear"; return false; }
bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) { string ns = dbname + "." + cmdObj.firstElement().valuestr(); NamespaceDetails *d = nsdetails(ns); if (NULL == d) { errmsg = "can't find ns"; return false; } GeoNearArguments commonArgs(cmdObj); if (commonArgs.numWanted < 0) { errmsg = "numWanted must be >= 0"; return false; } vector<int> idxs; d->findIndexByType("2d", idxs); if (idxs.size() > 1) { errmsg = "more than one 2d index, not sure which to run geoNear on"; return false; } if (1 == idxs.size()) { result.append("ns", ns); return run2DGeoNear(d->idx(idxs[0]), cmdObj, commonArgs, errmsg, result); } d->findIndexByType("2dsphere", idxs); if (idxs.size() > 1) { errmsg = "more than one 2dsphere index, not sure which to run geoNear on"; return false; } if (1 == idxs.size()) { result.append("ns", ns); return run2DSphereGeoNear(d->idx(idxs[0]), cmdObj, commonArgs, errmsg, result); } errmsg = "no geo indices for geoNear"; return false; }