20 #include <boost/noncopyable.hpp>
27 #include "mongo/bson/bsonelement.h"
28 #include "mongo/base/data_view.h"
29 #include "mongo/base/string_data.h"
30 #include "mongo/bson/util/builder.h"
31 #include "mongo/client/export_macros.h"
32 #include "mongo/platform/atomic_word.h"
33 #include "mongo/util/shared_buffer.h"
37 typedef std::set< BSONElement, BSONElementCmpWithoutField > BSONElementSet;
38 typedef std::multiset< BSONElement, BSONElementCmpWithoutField > BSONElementMSet;
86 static const char kEmptyObjectPrototype[] = { 5, 0, 0, 0, 0 };
87 _objdata = kEmptyObjectPrototype;
98 : _objdata(ownedBuffer.get() ? ownedBuffer.get() :
BSONObj().objdata())
99 , _ownedBuffer(ownedBuffer.moveFrom()) {
102 #if __cplusplus >= 201103L
104 BSONObj(BSONObj&& other)
105 : _objdata(std::move(other._objdata))
106 , _ownedBuffer(std::move(other._ownedBuffer)) {
107 other._objdata = BSONObj()._objdata;
114 BSONObj(
const BSONObj&) =
default;
121 this->swap(otherCopy);
128 swap(_objdata, other._objdata);
129 swap(_ownedBuffer, other._ownedBuffer);
160 bool isOwned()
const {
return _ownedBuffer.get() != 0; }
173 enum { maxToStringRecursionDepth = 100 };
175 std::string toString(
bool isArray =
false,
bool full=
false )
const;
176 void toString( StringBuilder& s,
bool isArray =
false,
bool full=
false,
int depth=0 )
const;
181 std::string jsonString(
188 int addFields(BSONObj& from, std::set<std::string>& fields);
193 BSONObj removeField(
const StringData& name)
const;
201 int getFieldNames(std::set<std::string>& fields)
const;
207 BSONElement getFieldDotted(
const StringData &name)
const;
213 void getFieldsDotted(
const StringData& name, BSONElementSet &ret,
bool expandLastArray =
true )
const;
214 void getFieldsDotted(
const StringData& name, BSONElementMSet &ret,
bool expandLastArray =
true )
const;
219 BSONElement getFieldDottedOrArray(
const char *&name)
const;
224 BSONElement getField(
const StringData& name)
const;
232 void getFields(
unsigned n,
const char **fieldNames, BSONElement *fields)
const;
238 return getField(field);
244 std::string s = ss.str();
245 return getField(s.c_str());
249 bool hasField(
const StringData& name )
const {
return !getField(name).eoo(); }
251 bool hasElement(
const StringData& name)
const {
return hasField(name); }
254 const char * getStringField(
const StringData& name)
const;
257 BSONObj getObjectField(
const StringData& name)
const;
260 int getIntField(
const StringData& name)
const;
265 bool getBoolField(
const StringData& name)
const;
286 BSONObj extractFields(
const BSONObj &pattern ,
bool fillWithNull=
false)
const;
288 BSONObj filterFieldsUndotted(
const BSONObj &filter,
bool inFilter)
const;
290 BSONElement getFieldUsingIndexNames(
const StringData& fieldName,
291 const BSONObj &indexKey)
const;
296 bool couldBeArray()
const;
305 return ConstDataView(objdata()).readLE<
int>();
311 return x > 0 && x <= BSONObjMaxInternalSize;
321 return _okForStorage(
false,
true).isOK();
331 return _okForStorage(
true,
true).isOK();
343 return _okForStorage(
false, deep);
355 return _okForStorage(
true, deep);
359 bool isEmpty()
const {
return objsize() <= 5; }
364 std::string hexDump()
const;
372 bool considerFieldName=
true)
const;
380 bool considerFieldName=
true)
const;
382 bool operator<(
const BSONObj& other )
const {
return woCompare( other ) < 0; }
383 bool operator<=(
const BSONObj& other )
const {
return woCompare( other ) <= 0; }
384 bool operator>(
const BSONObj& other )
const {
return woCompare( other ) > 0; }
385 bool operator>=(
const BSONObj& other )
const {
return woCompare( other ) >= 0; }
390 int woSortOrder(
const BSONObj& r ,
const BSONObj& sortKey ,
bool useDotted=
false )
const;
392 bool equal(
const BSONObj& r)
const;
400 size_t operator() (
const BSONObj& obj)
const;
409 bool isPrefixOf(
const BSONObj& otherObj )
const;
417 bool isFieldNamePrefixOf(
const BSONObj& otherObj )
const;
425 return (os == 0 || memcmp(objdata(),r.
objdata(),os)==0);
437 const char *p = objdata() + 4;
438 return *p ==
EOO ?
"" : p+1;
442 const char *p = objdata() + 4;
451 bool getObjectID(BSONElement& e)
const;
457 BSONObj clientReadable()
const;
461 BSONObj replaceFieldNames(
const BSONObj &obj )
const;
466 bool operator==(
const BSONObj& other )
const {
return equal( other ); }
467 bool operator!=(
const BSONObj& other)
const {
return !operator==( other); }
488 opMAX_DISTANCE = 0x15,
489 opGEO_INTERSECTS = 0x16,
493 void elems(std::vector<BSONElement> &)
const;
495 void elems(std::list<BSONElement> &)
const;
497 friend class BSONObjIterator;
498 typedef BSONObjIterator iterator;
506 BSONObjIterator begin()
const;
508 void appendSelfToBufBuilder(BufBuilder& b)
const {
510 b.appendBuf(objdata(), objsize());
513 template<
typename T>
bool coerceVector( std::vector<T>* out )
const;
515 typedef SharedBuffer::Holder Holder;
529 void _assertInvalid()
const;
531 void init(
const char *data) {
543 Status _okForStorage(
bool root,
bool deep)
const;
545 const char* _objdata;
546 SharedBuffer _ownedBuffer;
549 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<( std::ostream &s,
const BSONObj &o );
550 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<( std::ostream &s,
const BSONElement &e );
552 MONGO_CLIENT_API StringBuilder& MONGO_CLIENT_FUNC operator<<( StringBuilder &s,
const BSONObj &o );
553 MONGO_CLIENT_API StringBuilder& MONGO_CLIENT_FUNC operator<<( StringBuilder &s,
const BSONElement &e );
555 inline void swap(BSONObj& l, BSONObj& r) {
end of object
Definition: bsontypes.h:42
JsonStringFormat
Formatting mode for generating JSON from BSON.
Definition: oid.h:204
BSONElement firstElement() const
Definition: bsonobj.h:431
bool isOwned() const
A BSONObj can use a buffer it "owns" or one it does not.
Definition: bsonobj.h:160
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:53
int objsize() const
Definition: bsonobj.h:304
Definition: shared_buffer.h:25
bool hasField(const StringData &name) const
Definition: bsonobj.h:249
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
BSONObj(const char *bsonData)
Construct a BSONObj from data in the proper format.
Definition: bsonobj.h:93
static BSONObj takeOwnership(char *holderPrefixedData)
Given a pointer to a region of un-owned memory containing BSON data, prefixed by sufficient space for...
Definition: bsonobj.h:524
const char * objdata() const
Definition: bsonobj.h:299
bool okForStorage() const
Definition: bsonobj.h:320
bool hasElement(const StringData &name) const
Definition: bsonobj.h:251
strict RFC format
Definition: oid.h:206
BSONObj & operator=(BSONObj otherCopy)
Provide assignment semantics.
Definition: bsonobj.h:120
void swap(BSONObj &other)
Swap this BSONObj with 'other'.
Definition: bsonobj.h:126
bool isValid() const
performs a cursory check on the object's size only.
Definition: bsonobj.h:309
BSONType
the complete list of valid BSON types see also bsonspec.org
Definition: bsontypes.h:38
Status storageValid(const bool deep=true) const
Validates that this can be stored as a document (in a collection) See details above in okForStorageAs...
Definition: bsonobj.h:354
MONGO_CLIENT_API bool isArray(const StringData &str)
Tests whether the JSON string is an Array.
bool okForStorageAsRoot() const
Same as above with the following extra restrictions Not valid if:
Definition: bsonobj.h:330
static SharedBuffer takeOwnership(char *holderPrefixedData)
Given a pointer to a region of un-owned data, prefixed by sufficient space for a SharedBuffer::Holder...
Definition: shared_buffer.h:54
const char * firstElementFieldName() const
faster than firstElement().fieldName() - for the first element we can easily find the fieldname witho...
Definition: bsonobj.h:436
Status storageValidEmbedded(const bool deep=true) const
Validates that this can be stored as an embedded document See details above in okForStorage.
Definition: bsonobj.h:342
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
Definition: bsonobj.h:559
bool isEmpty() const
Definition: bsonobj.h:359
bool binaryEqual(const BSONObj &r) const
This is "shallow equality" – ints and doubles won't match.
Definition: bsonobj.h:422
BSONObj()
Construct an empty BSONObj – that is, {}.
Definition: bsonobj.h:82
A precomputation of a BSON index or sort key pattern.
Definition: ordering.h:32
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.
Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject ...
Definition: bsonobj.h:399