24#include "mongo/base/data_cursor.h"
25#include "mongo/bson/bsontypes.h"
26#include "mongo/bson/oid.h"
27#include "mongo/bson/timestamp.h"
28#include "mongo/client/export_macros.h"
29#include "mongo/platform/cstdint.h"
30#include "mongo/platform/float_utils.h"
31#include "mongo/platform/strnlen.h"
39int compareElementValues(
const BSONElement& l,
const BSONElement& r);
65 const StringData checkAndGetStringData()
const {
71 double Number()
const {
72 return chk(isNumber()).number();
74 double Double()
const {
75 return chk(NumberDouble)._numberDouble();
77 long long Long()
const {
78 return chk(NumberLong)._numberLong();
81 return chk(NumberInt)._numberInt();
86 std::vector<BSONElement>
Array()
const;
88 return chk(jstOID).__oid();
114 void Val(
long long& v)
const {
117 void Val(
bool& v)
const {
120 void Val(BSONObj& v)
const;
124 void Val(
int& v)
const {
127 void Val(
double& v)
const {
130 void Val(std::string& v)
const {
141 std::string toString(
bool includeFieldName =
true,
bool full =
false)
const;
143 bool includeFieldName =
true,
145 int depth = 0)
const;
147 bool includeFieldNames =
true,
148 int pretty = 0)
const;
149 operator std::string()
const {
155 const signed char typeByte =
ConstDataView(data).readLE<
signed char>();
156 return static_cast<BSONType>(typeByte);
173 return type() ==
EOO;
202 if (fieldNameSize_ == -1)
203 fieldNameSize_ = (int)strlen(fieldName()) + 1;
204 return fieldNameSize_;
207 const StringData fieldNameStringData()
const {
208 return StringData(fieldName(), eoo() ? 0 : fieldNameSize() - 1);
213 return (data + fieldNameSize() + 1);
217 return size() - fieldNameSize() - 1;
220 bool isBoolean()
const {
228 return *value() ? true :
false;
231 bool booleanSafe()
const {
232 return isBoolean() && boolean();
246 bool trueValue()
const;
249 bool isSimpleType()
const;
252 bool isNumber()
const;
270 int numberInt()
const;
274 long long numberLong()
const;
281 long long safeNumberLong()
const;
286 double numberDouble()
const;
291 return numberDouble();
297 return OID::from(value());
314 size_t objsize()
const {
331 return type() ==
mongo::String ? std::string(valuestr(), valuestrsize() - 1)
340 return StringData(valuestr(), valuestrsize() - 1);
345 massert(16177,
"not codeWScope", type() ==
CodeWScope);
346 return value() + 4 + 4;
352 massert(16178,
"not codeWScope", type() ==
CodeWScope);
365 return codeWScopeCode() + strlen(codeWScopeCode()) + 1;
374 const char* codeWScopeScopeData()
const {
375 return codeWScopeCode() + codeWScopeCodeLen();
382 BSONObj embeddedObjectUserCheck()
const;
384 BSONObj codeWScopeObject()
const;
390 len = valuestrsize();
396 if (binDataType() != ByteArrayDeprecated) {
400 len = valuestrsize() - 4;
401 return value() + 5 + 4;
405 BinDataType binDataType()
const {
407 verify(type() == BinData);
408 unsigned char c = (value() + 4)[0];
409 return (BinDataType)c;
414 verify(type() ==
RegEx);
420 const char* p = regex();
421 return p + strlen(p) + 1;
428 return woCompare(r,
false) == 0;
433 return woCompare(r,
true) == 0;
437 return !operator==(r);
456 const char* rawdata()
const {
491 const uint32_t increment = cursor.readLEAndAdvance<uint32_t>();
492 const uint32_t seconds = cursor.readLE<uint32_t>();
496 const char* dbrefNS()
const {
497 uassert(10063,
"not a dbref", type() == DBRef);
502 uassert(10064,
"not a dbref", type() == DBRef);
503 const char* start = value();
504 start += 4 + ConstDataView(start).readLE<
int>();
505 return mongo::OID::from(start);
515 return compareElementValues(*
this, other) < 0;
519 explicit BSONElement(
const char* d,
int maxLen) : data(d) {
527 size_t size = strnlen(fieldName(), maxLen - 1);
528 uassert(10333,
"Invalid field name", size <
size_t(maxLen - 1));
529 fieldNameSize_ = size + 1;
534 explicit BSONElement(
const char* d) : data(d) {
552 fieldNameSize_(fieldNameSize)
556 std::string _asCode()
const;
558 template <
typename T>
559 bool coerce(T* out)
const;
563 mutable int fieldNameSize_;
565 mutable int totalSize;
573 ss <<
"field not found, expected type " << t;
575 ss <<
"wrong type for field (" << fieldName() <<
") " << type() <<
" != " << t;
576 msgasserted(13111, ss.str());
580 const BSONElement& chk(
bool expr)
const {
581 massert(13118,
"unexpected or missing type value in BSON object", expr);
690 if (d > (
double)std::numeric_limits<long long>::max()) {
691 return std::numeric_limits<long long>::max();
693 if (d < std::numeric_limits<long long>::min()) {
694 return std::numeric_limits<long long>::min();
702 static const char kEooElement[] =
"";
709std::string escape(
const std::string& s,
bool escape_slash =
false);
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
bool isSimpleType() const
True if number, string, bool, date, OID.
Definition bsonelement.h:620
bool operator<(const BSONElement &other) const
this does not use fieldName in the comparison, just the value
Definition bsonelement.h:509
int valuesize() const
size in bytes of the element's value (when applicable).
Definition bsonelement.h:216
bool ok() const
Use ok() to check if a value is assigned: if( myObj["foo"].ok() ) ...
Definition bsonelement.h:137
long long numberLong() const
Retrieve long value for the element safely.
Definition bsonelement.h:664
bool isABSONObj() const
True if this element can be a BSONObj.
Definition bsonelement.h:479
const char * valuestr() const
Get a string's value.
Definition bsonelement.h:321
const char * codeWScopeScopeDataUnsafe() const
Get the scope SavedContext of a CodeWScope data element.
Definition bsonelement.h:363
const char * regex() const
Retrieve the regex string for a Regex element.
Definition bsonelement.h:413
const char * binData(int &len) const
Get raw binary data.
Definition bsonelement.h:387
BSONObj wrap(const StringData &newName) const
Wrap this element up as a singleton object with a new name.
mongo::OID __oid() const
Retrieve the object ID stored in the object.
Definition bsonelement.h:296
BSONObj wrap() const
Wrap this element up as a singleton object.
const char * binDataClean(int &len) const
Get binary data.
Definition bsonelement.h:394
int canonicalType() const
See canonicalizeBSONType in bsontypes.h.
Definition bsonelement.h:165
int _numberInt() const
Return int value for this field.
Definition bsonelement.h:260
double numberDouble() const
Retrieve the numeric value of the element.
Definition bsonelement.h:635
const char * value() const
raw data of the element's value (so be careful).
Definition bsonelement.h:212
BSONElement()
Constructs an empty element.
Definition bsonelement.h:701
double number() const
Retrieve the numeric value of the element.
Definition bsonelement.h:290
const char * fieldName() const
field name of the element.
Definition bsonelement.h:192
bool mayEncapsulate() const
True if this element may contain subobjects.
Definition bsonelement.h:467
const char * regexFlags() const
Retrieve the regex flags (options) for a Regex element.
Definition bsonelement.h:419
BSONElement(const char *d, int fieldNameSize, FieldNameSizeTag)
Construct a BSONElement where you already know the length of the name.
Definition bsonelement.h:550
bool isNumber() const
True if element is of a numeric type.
Definition bsonelement.h:609
const StringData valueStringData() const
Returns a StringData pointing into this element's data.
Definition bsonelement.h:339
int valuestrsize() const
Size (length) of a string element.
Definition bsonelement.h:309
bool boolean() const
Definition bsonelement.h:227
int fieldNameSize() const
NOTE: size includes the NULL terminator.
Definition bsonelement.h:201
bool isNull() const
True if element is null.
Definition bsonelement.h:301
int size(int maxLen) const
Size of the element.
bool operator!=(const BSONElement &r) const
Returns true if elements are unequal.
Definition bsonelement.h:436
int getGtLtOp(int def=0) const
0 == Equality, just not defined yet
const char * valuestrsafe() const
Get the string value of the element.
Definition bsonelement.h:326
int woCompare(const BSONElement &e, bool considerFieldName=true) const
Well ordered comparison.
bool trueValue() const
Convert the value to boolean, regardless of its type, in a javascript-like fashion (i....
Definition bsonelement.h:586
int codeWScopeCodeLen() const
Get length of the code part of the CodeWScope object This INCLUDES the null char at the end.
Definition bsonelement.h:351
long long safeNumberLong() const
Like numberLong() but with well-defined behavior for doubles that are NaNs, or too large/small to be ...
Definition bsonelement.h:682
bool operator==(const BSONElement &r) const
Returns true if elements are equal.
Definition bsonelement.h:432
const char * codeWScopeCode() const
Get javascript code of a CodeWScope data element.
Definition bsonelement.h:344
int numberInt() const
Retrieve int value for the element safely.
Definition bsonelement.h:650
BSONObj embeddedObject() const
Get the embedded object this element holds.
double _numberDouble() const
Return double value for this field.
Definition bsonelement.h:255
bool eoo() const
Indicates if it is the end-of-object element, which is present at the end of every BSON object.
Definition bsonelement.h:172
bool valuesEqual(const BSONElement &r) const
like operator== but doesn't check the fieldname, just the value.
Definition bsonelement.h:427
BSONElement operator[](const std::string &field) const
retrieve a field within this element throws exception if *this is not an embedded object
long long _numberLong() const
Return long long value for this field.
Definition bsonelement.h:265
void Val(Date_t &v) const
populate v with the value of the element.
Definition bsonelement.h:111
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:154
Date_t date() const
Retrieve a java style date value from the element.
Definition bsonelement.h:239
std::string str() const
Get the string value of the element.
Definition bsonelement.h:330
std::string String() const
These functions, which start with a capital letter, throw a MsgAssertionException if the element is n...
Definition bsonelement.h:62
iterator for a BSONObj
Definition bsonobjiterator.h:37
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
Definition data_cursor.h:26
Definition data_view.h:30
Object ID type.
Definition oid.h:60
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8
Definition builder.h:325
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents.
Definition string_data.h:43
Definition timestamp.h:23
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition oid.h:205
int canonicalizeBSONType(BSONType type)
Returns a number for where a given type falls in the sort order.
Definition bsontypes.h:109
BSONType
the complete list of valid BSON types see also bsonspec.org
Definition bsontypes.h:38
@ CodeWScope
javascript code that can execute on the database server, with SavedContext
Definition bsontypes.h:72
@ String
character string, stored in utf8
Definition bsontypes.h:46
@ BinData
binary data
Definition bsontypes.h:52
@ Array
an embedded array
Definition bsontypes.h:50
@ Bool
boolean type
Definition bsontypes.h:58
@ Timestamp
Updated to a Date with value next OpTime on insert.
Definition bsontypes.h:76
@ RegEx
regular expression, a pattern with options
Definition bsontypes.h:64
@ jstOID
ObjectId.
Definition bsontypes.h:56
@ Undefined
Undefined type.
Definition bsontypes.h:54
@ EOO
end of object
Definition bsontypes.h:42
@ Object
an embedded object
Definition bsontypes.h:48
@ NumberLong
64 bit integer
Definition bsontypes.h:78
@ jstNULL
null type
Definition bsontypes.h:62
@ NumberInt
32 bit signed integer
Definition bsontypes.h:74
@ NumberDouble
double precision floating point value
Definition bsontypes.h:44
@ Date
date type
Definition bsontypes.h:60
Definition bsonelement.h:543
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition bsonelement.h:452
Definition time_support.h:39