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"
39 int compareElementValues(
const BSONElement& l,
const BSONElement& r);
63 const StringData checkAndGetStringData()
const {
66 Date_t Date()
const {
return chk(
mongo::Date).date(); }
67 double Number()
const {
return chk(isNumber()).number(); }
68 double Double()
const {
return chk(NumberDouble)._numberDouble(); }
69 long long Long()
const {
return chk(NumberLong)._numberLong(); }
70 int Int()
const {
return chk(NumberInt)._numberInt(); }
72 std::vector<BSONElement>
Array()
const;
73 mongo::OID OID()
const {
return chk(jstOID).__oid(); }
74 void Null()
const { chk(isNull()); }
75 void OK()
const { chk(ok()); }
90 void Val(
long long& v)
const { v = Long(); }
91 void Val(
bool& v)
const { v = Bool(); }
92 void Val(BSONObj& v)
const;
94 void Val(
int& v)
const { v = Int(); }
95 void Val(
double& v)
const { v = Double(); }
96 void Val(std::string& v)
const { v =
String(); }
101 bool ok()
const {
return !eoo(); }
103 std::string toString(
bool includeFieldName =
true,
bool full=
false)
const;
104 void toString(
StringBuilder& s,
bool includeFieldName =
true,
bool full=
false,
int depth=0)
const;
105 std::string jsonString(
JsonStringFormat format,
bool includeFieldNames =
true,
int pretty = 0 )
const;
106 operator std::string()
const {
return toString(); }
110 const signed char typeByte = ConstDataView(data).readLE<
signed char>();
111 return static_cast<BSONType>(typeByte);
125 bool eoo()
const {
return type() ==
EOO; }
144 if ( eoo() )
return "";
152 if ( fieldNameSize_ == -1 )
153 fieldNameSize_ = (int)strlen( fieldName() ) + 1;
154 return fieldNameSize_;
157 const StringData fieldNameStringData()
const {
158 return StringData(fieldName(), eoo() ? 0 : fieldNameSize() - 1);
163 return (data + fieldNameSize() + 1);
167 return size() - fieldNameSize() - 1;
170 bool isBoolean()
const {
return type() ==
mongo::Bool; }
176 return *value() ? true :
false;
179 bool booleanSafe()
const {
return isBoolean() && boolean(); }
186 return Date_t(ConstDataView(value()).readLE<unsigned long long>());
192 bool trueValue()
const;
195 bool isSimpleType()
const;
198 bool isNumber()
const;
202 return ConstDataView(value()).readLE<
double>();
207 return ConstDataView(value()).readLE<
int>();
212 return ConstDataView(value()).readLE<
long long>();
216 int numberInt()
const;
220 long long numberLong()
const;
227 long long safeNumberLong()
const;
232 double numberDouble()
const;
236 double number()
const {
return numberDouble(); }
241 return OID::from(value());
254 return ConstDataView(value()).readLE<
int>();
258 size_t objsize()
const {
259 return ConstDataView(value()).readLE<uint32_t>();
275 return type() ==
mongo::String ? std::string(valuestr(), valuestrsize()-1) : std::string();
283 return StringData(valuestr(), valuestrsize() - 1);
288 massert( 16177 ,
"not codeWScope" , type() ==
CodeWScope );
289 return value() + 4 + 4;
295 massert( 16178 ,
"not codeWScope" , type() ==
CodeWScope );
296 return ConstDataView(value() + 4).readLE<
int>();
308 return codeWScopeCode() + strlen( codeWScopeCode() ) + 1;
317 const char * codeWScopeScopeData()
const {
318 return codeWScopeCode() + codeWScopeCodeLen();
325 BSONObj embeddedObjectUserCheck()
const;
327 BSONObj codeWScopeObject()
const;
333 len = valuestrsize();
339 if (binDataType() != ByteArrayDeprecated) {
344 len = valuestrsize() - 4;
345 return value() + 5 + 4;
349 BinDataType binDataType()
const {
351 verify( type() == BinData );
352 unsigned char c = (value() + 4)[0];
353 return (BinDataType)c;
358 verify(type() ==
RegEx);
364 const char *p = regex();
365 return p + strlen(p) + 1;
372 return woCompare( r ,
false ) == 0;
377 return woCompare( r ,
true ) == 0;
398 const char * rawdata()
const {
return data; }
430 ConstDataCursor cursor(value());
431 const uint32_t increment = cursor.readLEAndAdvance<uint32_t>();
432 const uint32_t seconds = cursor.readLE<uint32_t>();
436 const char * dbrefNS()
const {
437 uassert( 10063 ,
"not a dbref" , type() == DBRef );
442 uassert( 10064 ,
"not a dbref" , type() == DBRef );
443 const char * start = value();
444 start += 4 + ConstDataView(start).readLE<
int>();
445 return mongo::OID::from(start);
451 if ( x < 0 )
return true;
452 else if ( x > 0 )
return false;
453 return compareElementValues(*
this,other) < 0;
457 explicit BSONElement(
const char *d,
int maxLen) : data(d) {
465 if ( maxLen != -1 ) {
466 size_t size = strnlen( fieldName(), maxLen - 1 );
467 uassert( 10333 ,
"Invalid field name", size <
size_t(maxLen - 1) );
468 fieldNameSize_ = size + 1;
473 explicit BSONElement(
const char *d) : data(d) {
491 , fieldNameSize_(fieldNameSize)
495 std::string _asCode()
const;
497 template<
typename T>
bool coerce( T* out )
const;
501 mutable int fieldNameSize_;
503 mutable int totalSize;
511 ss <<
"field not found, expected type " << t;
513 ss <<
"wrong type for field (" << fieldName() <<
") " << type() <<
" != " << t;
514 msgasserted(13111, ss.str() );
518 const BSONElement& chk(
bool expr)
const {
519 massert(13118,
"unexpected or missing type value in BSON object", expr);
627 if ( d > (
double) std::numeric_limits<long long>::max() ){
628 return std::numeric_limits<long long>::max();
630 if ( d < std::numeric_limits<long long>::min() ){
631 return std::numeric_limits<long long>::min();
639 static const char kEooElement[] =
"";
646 std::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:558
bool operator<(const BSONElement &other) const
this does not use fieldName in the comparison, just the value
Definition bsonelement.h:449
int valuesize() const
size in bytes of the element's value (when applicable).
Definition bsonelement.h:166
bool ok() const
Use ok() to check if a value is assigned: if( myObj["foo"].ok() ) ...
Definition bsonelement.h:101
long long numberLong() const
Retrieve long value for the element safely.
Definition bsonelement.h:601
bool isABSONObj() const
True if this element can be a BSONObj.
Definition bsonelement.h:419
const char * valuestr() const
Get a string's value.
Definition bsonelement.h:265
const char * codeWScopeScopeDataUnsafe() const
Get the scope SavedContext of a CodeWScope data element.
Definition bsonelement.h:306
const char * regex() const
Retrieve the regex string for a Regex element.
Definition bsonelement.h:357
const char * binData(int &len) const
Get raw binary data.
Definition bsonelement.h:330
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:240
BSONObj wrap() const
Wrap this element up as a singleton object.
const char * binDataClean(int &len) const
Get binary data.
Definition bsonelement.h:337
int canonicalType() const
See canonicalizeBSONType in bsontypes.h.
Definition bsonelement.h:120
int _numberInt() const
Return int value for this field.
Definition bsonelement.h:206
double numberDouble() const
Retrieve the numeric value of the element.
Definition bsonelement.h:573
const char * value() const
raw data of the element's value (so be careful).
Definition bsonelement.h:162
BSONElement()
Constructs an empty element.
Definition bsonelement.h:638
double number() const
Retrieve the numeric value of the element.
Definition bsonelement.h:236
const char * fieldName() const
field name of the element.
Definition bsonelement.h:143
bool mayEncapsulate() const
True if this element may contain subobjects.
Definition bsonelement.h:407
const char * regexFlags() const
Retrieve the regex flags (options) for a Regex element.
Definition bsonelement.h:363
BSONElement(const char *d, int fieldNameSize, FieldNameSizeTag)
Construct a BSONElement where you already know the length of the name.
Definition bsonelement.h:489
bool isNumber() const
True if element is of a numeric type.
Definition bsonelement.h:547
const StringData valueStringData() const
Returns a StringData pointing into this element's data.
Definition bsonelement.h:282
int valuestrsize() const
Size (length) of a string element.
Definition bsonelement.h:253
bool boolean() const
Definition bsonelement.h:175
int fieldNameSize() const
NOTE: size includes the NULL terminator.
Definition bsonelement.h:151
bool isNull() const
True if element is null.
Definition bsonelement.h:245
int size(int maxLen) const
Size of the element.
bool operator!=(const BSONElement &r) const
Returns true if elements are unequal.
Definition bsonelement.h:380
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:270
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:524
int codeWScopeCodeLen() const
Get length of the code part of the CodeWScope object This INCLUDES the null char at the end.
Definition bsonelement.h:294
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:619
bool operator==(const BSONElement &r) const
Returns true if elements are equal.
Definition bsonelement.h:376
const char * codeWScopeCode() const
Get javascript code of a CodeWScope data element.
Definition bsonelement.h:287
int numberInt() const
Retrieve int value for the element safely.
Definition bsonelement.h:587
BSONObj embeddedObject() const
Get the embedded object this element holds.
double _numberDouble() const
Return double value for this field.
Definition bsonelement.h:201
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:125
bool valuesEqual(const BSONElement &r) const
like operator== but doesn't check the fieldname, just the value.
Definition bsonelement.h:371
long long _numberLong() const
Return long long value for this field.
Definition bsonelement.h:211
void Val(Date_t &v) const
populate v with the value of the element.
Definition bsonelement.h:89
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:109
Date_t date() const
Retrieve a java style date value from the element.
Definition bsonelement.h:185
std::string str() const
Get the string value of the element.
Definition bsonelement.h:274
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
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:299
Definition timestamp.h:23
the main MongoDB namespace
Definition bulk_operation_builder.h:24
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition oid.h:204
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:482
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition bsonelement.h:394
Definition time_support.h:39