diff options
Diffstat (limited to 'src/mongo/bson')
-rw-r--r-- | src/mongo/bson/util/bson_extract.cpp | 12 | ||||
-rw-r--r-- | src/mongo/bson/util/bson_extract.h | 15 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/mongo/bson/util/bson_extract.cpp b/src/mongo/bson/util/bson_extract.cpp index 84c9cc92de6..8a453c02472 100644 --- a/src/mongo/bson/util/bson_extract.cpp +++ b/src/mongo/bson/util/bson_extract.cpp @@ -185,6 +185,18 @@ Status bsonExtractDoubleField(const BSONObj& object, StringData fieldName, doubl return Status::OK(); } +Status bsonExtractDoubleFieldWithDefault(const BSONObj& object, + StringData fieldName, + double defaultValue, + double* out) { + Status status = bsonExtractDoubleField(object, fieldName, out); + if (status == ErrorCodes::NoSuchKey) { + *out = defaultValue; + status = Status::OK(); + } + return status; +} + Status bsonExtractIntegerFieldWithDefault(const BSONObj& object, StringData fieldName, long long defaultValue, diff --git a/src/mongo/bson/util/bson_extract.h b/src/mongo/bson/util/bson_extract.h index cedc84cd01f..a4e2b90d82d 100644 --- a/src/mongo/bson/util/bson_extract.h +++ b/src/mongo/bson/util/bson_extract.h @@ -158,6 +158,21 @@ Status bsonExtractIntegerFieldWithDefault(const BSONObj& object, long long* out); /** + * Finds a double-precision floating point element named "fieldName" in "object". + * + * If a field named "fieldName" is present, and is a double, stores the value of the field into + * "*out". If no field named fieldName is present, sets "*out" to "defaultValue". In these cases, + * returns Status::OK(). + * + * If "fieldName" is present more than once, behavior is undefined. If the found field is not a + * double, returns ErrorCodes::TypeMismatch. + */ +Status bsonExtractDoubleFieldWithDefault(const BSONObj& object, + StringData fieldName, + double defaultValue, + double* out); + +/** * Finds a std::string element named "fieldName" in "object". * * If a field named "fieldName" is present, and is a string, stores the value of the field into |