本文整理汇总了C++中BSONElement::numberDouble方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONElement::numberDouble方法的具体用法?C++ BSONElement::numberDouble怎么用?C++ BSONElement::numberDouble使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONElement
的用法示例。
在下文中一共展示了BSONElement::numberDouble方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: matchesSingleElement
bool ComparisonMatchExpression::matchesSingleElement(const BSONElement& e) const {
if (e.canonicalType() != _rhs.canonicalType()) {
// some special cases
// jstNULL and undefined are treated the same
if (e.canonicalType() + _rhs.canonicalType() == 5) {
return matchType() == EQ || matchType() == LTE || matchType() == GTE;
}
if (_rhs.type() == MaxKey || _rhs.type() == MinKey) {
return matchType() != EQ;
}
return false;
}
// Special case handling for NaN. NaN is equal to NaN but
// otherwise always compares to false.
if (std::isnan(e.numberDouble()) || std::isnan(_rhs.numberDouble())) {
bool bothNaN = std::isnan(e.numberDouble()) && std::isnan(_rhs.numberDouble());
switch (matchType()) {
case LT:
return false;
case LTE:
return bothNaN;
case EQ:
return bothNaN;
case GT:
return false;
case GTE:
return bothNaN;
default:
// This is a comparison match expression, so it must be either
// a $lt, $lte, $gt, $gte, or equality expression.
fassertFailed(17448);
}
}
int x = compareElementValues(e, _rhs, _collator);
switch (matchType()) {
case LT:
return x < 0;
case LTE:
return x <= 0;
case EQ:
return x == 0;
case GT:
return x > 0;
case GTE:
return x >= 0;
default:
// This is a comparison match expression, so it must be either
// a $lt, $lte, $gt, $gte, or equality expression.
fassertFailed(16828);
}
}
示例2: run
bool run(const string& dbname, BSONObj& cmdObj, int,
string& errmsg, BSONObjBuilder& result, bool fromRepl) {
string ns = dbname + "." + cmdObj.firstElement().valuestr();
NamespaceDetails *nsd = nsdetails(ns);
if (NULL == nsd) {
errmsg = "can't find ns";
return false;
}
vector<int> idxs;
nsd->findIndexByType(GEOSEARCHNAME, idxs);
if (idxs.size() == 0) {
errmsg = "no geoSearch index";
return false;
}
if (idxs.size() > 1) {
errmsg = "more than 1 geosearch index";
return false;
}
BSONElement nearElt = cmdObj["near"];
BSONElement maxDistance = cmdObj["maxDistance"];
BSONElement search = cmdObj["search"];
uassert(13318, "near needs to be an array", nearElt.isABSONObj());
uassert(13319, "maxDistance needs a number", maxDistance.isNumber());
uassert(13320, "search needs to be an object", search.type() == Object);
unsigned limit = 50;
if (cmdObj["limit"].isNumber())
limit = static_cast<unsigned>(cmdObj["limit"].numberInt());
int idxNum = idxs[0];
IndexDetails& id = nsd->idx(idxNum);
if (CatalogHack::testIndexMigration()) {
auto_ptr<IndexDescriptor> desc(CatalogHack::getDescriptor(nsd, idxNum));
auto_ptr<HaystackAccessMethod> ham(new HaystackAccessMethod(desc.get()));
ham->searchCommand(nearElt.Obj(), maxDistance.numberDouble(), search.Obj(),
&result, limit);
} else {
GeoHaystackSearchIndex *si =
static_cast<GeoHaystackSearchIndex*>(id.getSpec().getType());
verify(&id == si->getDetails());
si->searchCommand(nsd, nearElt.Obj(), maxDistance.numberDouble(), search.Obj(),
result, limit);
}
return 1;
}
示例3: i
GeoHaystackSearchIndex( const IndexPlugin* plugin , const IndexSpec* spec )
: IndexType( plugin , spec ) {
BSONElement e = spec->info["bucketSize"];
uassert( 13321 , "need bucketSize" , e.isNumber() );
_bucketSize = e.numberDouble();
BSONObjBuilder orderBuilder;
BSONObjIterator i( spec->keyPattern );
while ( i.more() ) {
BSONElement e = i.next();
if ( e.type() == String && GEOSEARCHNAME == e.valuestr() ) {
uassert( 13314 , "can't have 2 geo fields" , _geo.size() == 0 );
uassert( 13315 , "2d has to be first in index" , _other.size() == 0 );
_geo = e.fieldName();
}
else {
_other.push_back( e.fieldName() );
}
orderBuilder.append( "" , 1 );
}
uassert( 13316 , "no geo field specified" , _geo.size() );
uassert( 13317 , "no other fields specified" , _other.size() );
uassert( 13326 , "quadrant search can only have 1 other field for now" , _other.size() == 1 );
_order = orderBuilder.obj();
}
示例4: set
virtual Status set(const BSONElement& newValueElement) {
long long newValue;
if (!newValueElement.isNumber()) {
StringBuilder sb;
sb << "Expected number type for journalCommitInterval via setParameter command: "
<< newValueElement;
return Status(ErrorCodes::BadValue, sb.str());
}
if (newValueElement.type() == NumberDouble &&
(newValueElement.numberDouble() - newValueElement.numberLong()) > 0) {
StringBuilder sb;
sb << "journalCommitInterval must be a whole number: "
<< newValueElement;
return Status(ErrorCodes::BadValue, sb.str());
}
newValue = newValueElement.numberLong();
if (newValue <= 1 || newValue >= 500) {
StringBuilder sb;
sb << "journalCommitInterval must be between 1 and 500, but attempted to set to: "
<< newValue;
return Status(ErrorCodes::BadValue, sb.str());
}
storageGlobalParams.journalCommitInterval = static_cast<unsigned>(newValue);
return Status::OK();
}
示例5: hashHaystackElement
// static
int ExpressionKeysPrivate::hashHaystackElement(const BSONElement& e, double bucketSize) {
uassert(16776, "geo field is not a number", e.isNumber());
double d = e.numberDouble();
d += 180;
d /= bucketSize;
return static_cast<int>(d);
}
示例6: parseHaystackParams
void ExpressionParams::parseHaystackParams(const BSONObj& infoObj,
std::string* geoFieldOut,
std::vector<std::string>* otherFieldsOut,
double* bucketSizeOut) {
BSONElement e = infoObj["bucketSize"];
uassert(16777, "need bucketSize", e.isNumber());
*bucketSizeOut = e.numberDouble();
uassert(16769, "bucketSize cannot be zero", *bucketSizeOut != 0.0);
// Example:
// db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })
BSONObjIterator i(infoObj.getObjectField("key"));
while (i.more()) {
BSONElement e = i.next();
if (e.type() == String && IndexNames::GEO_HAYSTACK == e.valuestr()) {
uassert(16770, "can't have more than one geo field", geoFieldOut->size() == 0);
uassert(16771, "the geo field has to be first in index", otherFieldsOut->size() == 0);
*geoFieldOut = e.fieldName();
} else {
uassert(16772,
"geoSearch can only have 1 non-geo field for now",
otherFieldsOut->size() == 0);
otherFieldsOut->push_back(e.fieldName());
}
}
}
示例7: i
GeoHaystackSearchIndex(const IndexPlugin* plugin, const IndexSpec* spec)
: IndexType(plugin, spec) {
BSONElement e = spec->info["bucketSize"];
uassert(13321, "need bucketSize", e.isNumber());
_bucketSize = e.numberDouble();
uassert(16455, "bucketSize cannot be zero", _bucketSize != 0.0);
// Example:
// db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })
BSONObjIterator i(spec->keyPattern);
while (i.more()) {
BSONElement e = i.next();
if (e.type() == String && GEOSEARCHNAME == e.valuestr()) {
uassert(13314, "can't have more than one geo field", _geoField.size() == 0);
uassert(13315, "the geo field has to be first in index",
_otherFields.size() == 0);
_geoField = e.fieldName();
} else {
// TODO(hk): Do we want to do any checking on e.type and e.valuestr?
uassert(13326, "geoSearch can only have 1 non-geo field for now",
_otherFields.size() == 0);
_otherFields.push_back(e.fieldName());
}
}
uassert(13316, "no geo field specified", _geoField.size());
// XXX: Fix documentation that says the other field is optional; code says it's mandatory.
uassert(13317, "no non-geo fields specified", _otherFields.size());
}
示例8:
// static
StatusWith<BSONObj> S2AccessMethod::fixSpec(const BSONObj& specObj) {
// If the spec object has the field "2dsphereIndexVersion", validate it. If it doesn't, add
// {2dsphereIndexVersion: 3}, which is the default for newly-built indexes.
BSONElement indexVersionElt = specObj[kIndexVersionFieldName];
if (indexVersionElt.eoo()) {
BSONObjBuilder bob;
bob.appendElements(specObj);
bob.append(kIndexVersionFieldName, S2_INDEX_VERSION_3);
return bob.obj();
}
if (!indexVersionElt.isNumber()) {
return {ErrorCodes::CannotCreateIndex,
str::stream() << "Invalid type for geo index version { " << kIndexVersionFieldName
<< " : "
<< indexVersionElt
<< " }, only versions: ["
<< S2_INDEX_VERSION_1
<< ","
<< S2_INDEX_VERSION_2
<< ","
<< S2_INDEX_VERSION_3
<< "] are supported"};
}
if (indexVersionElt.type() == BSONType::NumberDouble &&
!std::isnormal(indexVersionElt.numberDouble())) {
return {ErrorCodes::CannotCreateIndex,
str::stream() << "Invalid value for geo index version { " << kIndexVersionFieldName
<< " : "
<< indexVersionElt
<< " }, only versions: ["
<< S2_INDEX_VERSION_1
<< ","
<< S2_INDEX_VERSION_2
<< ","
<< S2_INDEX_VERSION_3
<< "] are supported"};
}
const auto indexVersion = indexVersionElt.numberLong();
if (indexVersion != S2_INDEX_VERSION_1 && indexVersion != S2_INDEX_VERSION_2 &&
indexVersion != S2_INDEX_VERSION_3) {
return {ErrorCodes::CannotCreateIndex,
str::stream() << "unsupported geo index version { " << kIndexVersionFieldName
<< " : "
<< indexVersionElt
<< " }, only versions: ["
<< S2_INDEX_VERSION_1
<< ","
<< S2_INDEX_VERSION_2
<< ","
<< S2_INDEX_VERSION_3
<< "] are supported"};
}
return specObj;
}
示例9: readMultiDeleteProperty
bool readMultiDeleteProperty(const BSONElement& limitElement) {
// Using a double to avoid throwing away illegal fractional portion. Don't want to accept 0.5
// here
const double limit = limitElement.numberDouble();
uassert(ErrorCodes::FailedToParse,
str::stream() << "The limit field in delete objects must be 0 or 1. Got " << limit,
limit == 0 || limit == 1);
return limit == 0;
}
示例10: errmsgRun
bool errmsgRun(OperationContext* opCtx,
const string& dbname,
const BSONObj& cmdObj,
string& errmsg,
BSONObjBuilder& result) {
const NamespaceString nss = CommandHelpers::parseNsCollectionRequired(dbname, cmdObj);
AutoGetCollectionForReadCommand ctx(opCtx, nss);
// Check whether we are allowed to read from this node after acquiring our locks.
auto replCoord = repl::ReplicationCoordinator::get(opCtx);
uassertStatusOK(replCoord->checkCanServeReadsFor(
opCtx, nss, ReadPreferenceSetting::get(opCtx).canRunOnSecondary()));
Collection* collection = ctx.getCollection();
if (!collection) {
errmsg = "can't find ns";
return false;
}
vector<IndexDescriptor*> idxs;
collection->getIndexCatalog()->findIndexByType(opCtx, IndexNames::GEO_HAYSTACK, idxs);
if (idxs.size() == 0) {
errmsg = "no geoSearch index";
return false;
}
if (idxs.size() > 1) {
errmsg = "more than 1 geosearch index";
return false;
}
BSONElement nearElt = cmdObj["near"];
BSONElement maxDistance = cmdObj["maxDistance"];
BSONElement search = cmdObj["search"];
uassert(13318, "near needs to be an array", nearElt.isABSONObj());
uassert(13319, "maxDistance needs a number", maxDistance.isNumber());
uassert(13320, "search needs to be an object", search.type() == Object);
unsigned limit = 50;
if (cmdObj["limit"].isNumber())
limit = static_cast<unsigned>(cmdObj["limit"].numberInt());
IndexDescriptor* desc = idxs[0];
HaystackAccessMethod* ham =
static_cast<HaystackAccessMethod*>(collection->getIndexCatalog()->getIndex(desc));
ham->searchCommand(opCtx,
collection,
nearElt.Obj(),
maxDistance.numberDouble(),
search.Obj(),
&result,
limit);
return 1;
}
示例11: run
bool run(OperationContext* txn,
const string& dbname,
BSONObj& cmdObj,
int,
string& errmsg,
BSONObjBuilder& result) {
const std::string ns = parseNsCollectionRequired(dbname, cmdObj);
AutoGetCollectionForRead ctx(txn, ns);
Collection* collection = ctx.getCollection();
if (!collection) {
errmsg = "can't find ns";
return false;
}
vector<IndexDescriptor*> idxs;
collection->getIndexCatalog()->findIndexByType(txn, IndexNames::GEO_HAYSTACK, idxs);
if (idxs.size() == 0) {
errmsg = "no geoSearch index";
return false;
}
if (idxs.size() > 1) {
errmsg = "more than 1 geosearch index";
return false;
}
BSONElement nearElt = cmdObj["near"];
BSONElement maxDistance = cmdObj["maxDistance"];
BSONElement search = cmdObj["search"];
uassert(13318, "near needs to be an array", nearElt.isABSONObj());
uassert(13319, "maxDistance needs a number", maxDistance.isNumber());
uassert(13320, "search needs to be an object", search.type() == Object);
unsigned limit = 50;
if (cmdObj["limit"].isNumber())
limit = static_cast<unsigned>(cmdObj["limit"].numberInt());
IndexDescriptor* desc = idxs[0];
HaystackAccessMethod* ham =
static_cast<HaystackAccessMethod*>(collection->getIndexCatalog()->getIndex(desc));
ham->searchCommand(txn,
collection,
nearElt.Obj(),
maxDistance.numberDouble(),
search.Obj(),
&result,
limit);
return 1;
}
示例12: bsonExtractDoubleField
Status bsonExtractDoubleField(const BSONObj& object, StringData fieldName, double* out) {
BSONElement value;
Status status = bsonExtractField(object, fieldName, &value);
if (!status.isOK())
return status;
if (!value.isNumber()) {
return Status(ErrorCodes::TypeMismatch,
mongoutils::str::stream() << "Expected field \"" << fieldName
<< "\" to have numeric type, but found "
<< typeName(value.type()));
}
*out = value.numberDouble();
return Status::OK();
}
示例13: run
bool run(OperationContext* txn, const string& dbname, BSONObj& cmdObj, int,
string& errmsg, BSONObjBuilder& result, bool fromRepl) {
const string ns = dbname + "." + cmdObj.firstElement().valuestr();
Client::ReadContext ctx(ns);
Database* db = ctx.ctx().db();
if ( !db ) {
errmsg = "can't find ns";
return false;
}
Collection* collection = db->getCollection( ns );
if ( !collection ) {
errmsg = "can't find ns";
return false;
}
vector<IndexDescriptor*> idxs;
collection->getIndexCatalog()->findIndexByType(IndexNames::GEO_HAYSTACK, idxs);
if (idxs.size() == 0) {
errmsg = "no geoSearch index";
return false;
}
if (idxs.size() > 1) {
errmsg = "more than 1 geosearch index";
return false;
}
BSONElement nearElt = cmdObj["near"];
BSONElement maxDistance = cmdObj["maxDistance"];
BSONElement search = cmdObj["search"];
uassert(13318, "near needs to be an array", nearElt.isABSONObj());
uassert(13319, "maxDistance needs a number", maxDistance.isNumber());
uassert(13320, "search needs to be an object", search.type() == Object);
unsigned limit = 50;
if (cmdObj["limit"].isNumber())
limit = static_cast<unsigned>(cmdObj["limit"].numberInt());
IndexDescriptor* desc = idxs[0];
HaystackAccessMethod* ham =
static_cast<HaystackAccessMethod*>( collection->getIndexCatalog()->getIndex(desc) );
ham->searchCommand(nearElt.Obj(), maxDistance.numberDouble(), search.Obj(),
&result, limit);
return 1;
}
示例14: run
bool run(const string& dbname , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
string ns = dbname + "." + cmdObj.firstElement().valuestr();
NamespaceDetails * d = nsdetails( ns.c_str() );
if ( ! d ) {
errmsg = "can't find ns";
return false;
}
vector<int> idxs;
d->findIndexByType( GEOSEARCHNAME , idxs );
if ( idxs.size() == 0 ) {
errmsg = "no geoSearch index";
return false;
}
if ( idxs.size() > 1 ) {
errmsg = "more than 1 geosearch index";
return false;
}
int idxNum = idxs[0];
IndexDetails& id = d->idx( idxNum );
GeoHaystackSearchIndex * si = (GeoHaystackSearchIndex*)id.getSpec().getType();
verify( &id == si->getDetails() );
BSONElement n = cmdObj["near"];
BSONElement maxDistance = cmdObj["maxDistance"];
BSONElement search = cmdObj["search"];
uassert( 13318 , "near needs to be an array" , n.isABSONObj() );
uassert( 13319 , "maxDistance needs a number" , maxDistance.isNumber() );
uassert( 13320 , "search needs to be an object" , search.type() == Object );
unsigned limit = 50;
if ( cmdObj["limit"].isNumber() )
limit = (unsigned)cmdObj["limit"].numberInt();
si->searchCommand( d , idxNum , n.Obj() , maxDistance.numberDouble() , search.Obj() , result , limit );
return 1;
}
示例15: matchesSingleElement
bool BitTestMatchExpression::matchesSingleElement(const BSONElement& e,
MatchDetails* details) const {
// Validate 'e' is a number or a BinData.
if (!e.isNumber() && e.type() != BSONType::BinData) {
return false;
}
if (e.type() == BSONType::BinData) {
int eBinaryLen; // Length of eBinary (in bytes).
const char* eBinary = e.binData(eBinaryLen);
return performBitTest(eBinary, eBinaryLen);
}
invariant(e.isNumber());
if (e.type() == BSONType::NumberDouble) {
double eDouble = e.numberDouble();
// NaN doubles are rejected.
if (std::isnan(eDouble)) {
return false;
}
// Integral doubles that are too large or small to be represented as a 64-bit signed
// integer are treated as 0. We use 'kLongLongMaxAsDouble' because if we just did
// eDouble > 2^63-1, it would be compared against 2^63. eDouble=2^63 would not get caught
// that way.
if (eDouble >= BSONElement::kLongLongMaxPlusOneAsDouble ||
eDouble < std::numeric_limits<long long>::min()) {
return false;
}
// This checks if e is an integral double.
if (eDouble != static_cast<double>(static_cast<long long>(eDouble))) {
return false;
}
}
long long eValue = e.numberLong();
return performBitTest(eValue);
}