MongoDB C++ Driver legacy-1.0.0
Loading...
Searching...
No Matches
bsonobjbuilder.h
1/* bsonobjbuilder.h
2
3 Classes in this file:
4 BSONObjBuilder
5 BSONArrayBuilder
6*/
7
8/* Copyright 2009 10gen Inc.
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#pragma once
24
25#include <boost/static_assert.hpp>
26#include <map>
27#include <cmath>
28#include <limits>
29
30#include "mongo/base/data_cursor.h"
31#include "mongo/base/parse_number.h"
32#include "mongo/bson/bson_field.h"
33#include "mongo/bson/bsonelement.h"
34#include "mongo/bson/bsonmisc.h"
35#include "mongo/bson/bsonobj.h"
36#include "mongo/bson/timestamp.h"
37#include "mongo/client/export_macros.h"
38
39namespace mongo {
40
41#if defined(_WIN32)
42// warning: 'this' : used in base member initializer list
43#pragma warning( disable : 4355 )
44#endif
45
49 class MONGO_CLIENT_API BSONObjBuilder : boost::noncopyable {
50 public:
52 BSONObjBuilder(int initsize=512)
53 : _b(_buf)
54 , _buf(sizeof(BSONObj::Holder) + initsize)
55 , _offset(sizeof(BSONObj::Holder))
56 , _s(this)
57 , _tracker(0)
58 , _doneCalled(false) {
59 // Reserve space for a holder object at the beginning of the buffer, followed by
60 // space for the object length. The length is filled in by _done.
61 _b.skip(sizeof(BSONObj::Holder));
62 _b.skip(sizeof(int));
63 }
64
69 : _b(baseBuilder)
70 , _buf(0)
71 , _offset(baseBuilder.len())
72 , _s(this)
73 , _tracker(0)
74 , _doneCalled(false) {
75 // Reserve space for the object length, which is filled in by _done. We don't need a holder
76 // since we are a sub-builder, and some parent builder has already made the reservation.
77 _b.skip(sizeof(int));
78 }
79
80 BSONObjBuilder( const BSONSizeTracker & tracker )
81 : _b(_buf)
82 , _buf(sizeof(BSONObj::Holder) + tracker.getSize())
83 , _offset(sizeof(BSONObj::Holder))
84 , _s(this)
85 , _tracker(const_cast<BSONSizeTracker*>(&tracker))
86 , _doneCalled(false) {
87 // See the comments in the first constructor for details.
88 _b.skip(sizeof(BSONObj::Holder));
89 _b.skip(sizeof(int));
90 }
91
92 ~BSONObjBuilder() {
93 // If 'done' has not already been called, and we have a reference to an owning
94 // BufBuilder but do not own it ourselves, then we must call _done to write in the
95 // length. Otherwise, we own this memory and its lifetime ends with us, therefore
96 // we can elide the write.
97 if ( !_doneCalled && _b.buf() && _buf.getSize() == 0 ) {
98 _done();
99 }
100 }
101
104
107
110 verify( !e.eoo() ); // do not append eoo, that would corrupt us. the builder auto appends when done() is called.
111 _b.appendBuf((void*) e.rawdata(), e.size());
112 return *this;
113 }
114
116 BSONObjBuilder& appendAs(const BSONElement& e, const StringData& fieldName) {
117 verify( !e.eoo() ); // do not append eoo, that would corrupt us. the builder auto appends when done() is called.
118 _b.appendNum((char) e.type());
119 _b.appendStr(fieldName);
120 _b.appendBuf((void *) e.value(), e.valuesize());
121 return *this;
122 }
123
125 BSONObjBuilder& append(const StringData& fieldName, BSONObj subObj) {
126 _b.appendNum((char) Object);
127 _b.appendStr(fieldName);
128 _b.appendBuf((void *) subObj.objdata(), subObj.objsize());
129 return *this;
130 }
131
133 BSONObjBuilder& appendObject(const StringData& fieldName, const char * objdata , int size = 0 ) {
134 verify( objdata );
135 if ( size == 0 ) {
136 size = ConstDataView(objdata).readLE<int>();
137 }
138
139 verify( size > 4 && size < 100000000 );
140
141 _b.appendNum((char) Object);
142 _b.appendStr(fieldName);
143 _b.appendBuf((void*)objdata, size );
144 return *this;
145 }
146
158 BufBuilder &subobjStart(const StringData& fieldName) {
159 _b.appendNum((char) Object);
160 _b.appendStr(fieldName);
161 return _b;
162 }
163
167 BSONObjBuilder& appendArray(const StringData& fieldName, const BSONObj &subObj) {
168 _b.appendNum((char) Array);
169 _b.appendStr(fieldName);
170 _b.appendBuf((void *) subObj.objdata(), subObj.objsize());
171 return *this;
172 }
173 BSONObjBuilder& append(const StringData& fieldName, BSONArray arr) {
174 return appendArray(fieldName, arr);
175 }
176
179 BufBuilder &subarrayStart(const StringData& fieldName) {
180 _b.appendNum((char) Array);
181 _b.appendStr(fieldName);
182 return _b;
183 }
184
186 BSONObjBuilder& appendBool(const StringData& fieldName, int val) {
187 _b.appendNum((char) Bool);
188 _b.appendStr(fieldName);
189 _b.appendNum((char) (val?1:0));
190 return *this;
191 }
192
194 BSONObjBuilder& append(const StringData& fieldName, bool val) {
195 _b.appendNum((char) Bool);
196 _b.appendStr(fieldName);
197 _b.appendNum((char) (val?1:0));
198 return *this;
199 }
200
202 BSONObjBuilder& append(const StringData& fieldName, int n) {
203 _b.appendNum((char) NumberInt);
204 _b.appendStr(fieldName);
205 _b.appendNum(n);
206 return *this;
207 }
208
210 BSONObjBuilder& append(const StringData& fieldName, unsigned n) {
211 return append(fieldName, (int) n);
212 }
213
215 BSONObjBuilder& append(const StringData& fieldName, long long n) {
216 _b.appendNum((char) NumberLong);
217 _b.appendStr(fieldName);
218 _b.appendNum(n);
219 return *this;
220 }
221
223 BSONObjBuilder& appendIntOrLL( const StringData& fieldName , long long n ) {
224 // extra () to avoid max macro on windows
225 static const long long maxInt = (std::numeric_limits<int>::max)() / 2;
226 static const long long minInt = -maxInt;
227 if ( minInt < n && n < maxInt ) {
228 append( fieldName , static_cast<int>( n ) );
229 }
230 else {
231 append( fieldName , n );
232 }
233 return *this;
234 }
235
240 BSONObjBuilder& appendNumber( const StringData& fieldName , int n ) {
241 return append( fieldName , n );
242 }
243
244 BSONObjBuilder& appendNumber( const StringData& fieldName , double d ) {
245 return append( fieldName , d );
246 }
247
248 BSONObjBuilder& appendNumber( const StringData& fieldName , size_t n ) {
249 static const size_t maxInt = ( 1 << 30 );
250
251 if ( n < maxInt )
252 append( fieldName, static_cast<int>( n ) );
253 else
254 append( fieldName, static_cast<long long>( n ) );
255 return *this;
256 }
257
258 BSONObjBuilder& appendNumber( const StringData& fieldName, long long llNumber ) {
259 static const long long maxInt = ( 1LL << 30 );
260 static const long long minInt = -maxInt;
261 static const long long maxDouble = ( 1LL << 40 );
262 static const long long minDouble = -maxDouble;
263
264 if ( minInt < llNumber && llNumber < maxInt ) {
265 append( fieldName, static_cast<int>( llNumber ) );
266 }
267 else if ( minDouble < llNumber && llNumber < maxDouble ) {
268 append( fieldName, static_cast<double>( llNumber ) );
269 }
270 else {
271 append( fieldName, llNumber );
272 }
273
274 return *this;
275 }
276
278 BSONObjBuilder& append(const StringData& fieldName, double n) {
279 _b.appendNum((char) NumberDouble);
280 _b.appendStr(fieldName);
281 _b.appendNum(n);
282 return *this;
283 }
284
288 bool appendAsNumber( const StringData& fieldName , const std::string& data );
289
294 BSONObjBuilder& appendOID(const StringData& fieldName, OID *oid = 0 , bool generateIfBlank = false ) {
295 _b.appendNum((char) jstOID);
296 _b.appendStr(fieldName);
297 if ( oid )
298 _b.appendBuf( oid->view().view(), OID::kOIDSize );
299 else {
300 OID tmp;
301 if ( generateIfBlank )
302 tmp.init();
303 else
304 tmp.clear();
305 _b.appendBuf( tmp.view().view(), OID::kOIDSize );
306 }
307 return *this;
308 }
309
315 BSONObjBuilder& append( const StringData& fieldName, OID oid ) {
316 _b.appendNum((char) jstOID);
317 _b.appendStr(fieldName);
318 _b.appendBuf( oid.view().view(), OID::kOIDSize );
319 return *this;
320 }
321
327 return append("_id", OID::gen());
328 }
329
334 BSONObjBuilder& appendTimeT(const StringData& fieldName, time_t dt) {
335 _b.appendNum((char) Date);
336 _b.appendStr(fieldName);
337 _b.appendNum(static_cast<unsigned long long>(dt) * 1000);
338 return *this;
339 }
344 BSONObjBuilder& appendDate(const StringData& fieldName, Date_t dt);
345 BSONObjBuilder& append(const StringData& fieldName, Date_t dt) {
346 return appendDate(fieldName, dt);
347 }
348
353 BSONObjBuilder& appendRegex(const StringData& fieldName, const StringData& regex, const StringData& options = "") {
354 _b.appendNum((char) RegEx);
355 _b.appendStr(fieldName);
356 _b.appendStr(regex);
357 _b.appendStr(options);
358 return *this;
359 }
360
361 BSONObjBuilder& append(const StringData& fieldName, const BSONRegEx& regex) {
362 return appendRegex(fieldName, regex.pattern, regex.flags);
363 }
364
365 BSONObjBuilder& appendCode(const StringData& fieldName, const StringData& code) {
366 _b.appendNum((char) Code);
367 _b.appendStr(fieldName);
368 _b.appendNum((int) code.size()+1);
369 _b.appendStr(code);
370 return *this;
371 }
372
373 BSONObjBuilder& append(const StringData& fieldName, const BSONCode& code) {
374 return appendCode(fieldName, code.code);
375 }
376
379 BSONObjBuilder& append(const StringData& fieldName, const char *str, int sz) {
380 _b.appendNum((char) String);
381 _b.appendStr(fieldName);
382 _b.appendNum((int)sz);
383 _b.appendBuf(str, sz);
384 return *this;
385 }
387 BSONObjBuilder& append(const StringData& fieldName, const char *str) {
388 return append(fieldName, str, (int) strlen(str)+1);
389 }
391 BSONObjBuilder& append(const StringData& fieldName, const std::string& str) {
392 return append(fieldName, str.c_str(), (int) str.size()+1);
393 }
395 BSONObjBuilder& append(const StringData& fieldName, const StringData& str) {
396 _b.appendNum((char) String);
397 _b.appendStr(fieldName);
398 _b.appendNum((int)str.size()+1);
399 _b.appendStr(str, true);
400 return *this;
401 }
402
403 BSONObjBuilder& appendSymbol(const StringData& fieldName, const StringData& symbol) {
404 _b.appendNum((char) Symbol);
405 _b.appendStr(fieldName);
406 _b.appendNum((int) symbol.size()+1);
407 _b.appendStr(symbol);
408 return *this;
409 }
410
411 BSONObjBuilder& append(const StringData& fieldName, const BSONSymbol& symbol) {
412 return appendSymbol(fieldName, symbol.symbol);
413 }
414
416 void appendNull() {
417 msgasserted(16234, "Invalid call to appendNull in BSONObj Builder.");
418 }
419
421 BSONObjBuilder& appendNull( const StringData& fieldName ) {
422 _b.appendNum( (char) jstNULL );
423 _b.appendStr( fieldName );
424 return *this;
425 }
426
427 // Append an element that is less than all other keys.
428 BSONObjBuilder& appendMinKey( const StringData& fieldName ) {
429 _b.appendNum( (char) MinKey );
430 _b.appendStr( fieldName );
431 return *this;
432 }
433 // Append an element that is greater than all other keys.
434 BSONObjBuilder& appendMaxKey( const StringData& fieldName ) {
435 _b.appendNum( (char) MaxKey );
436 _b.appendStr( fieldName );
437 return *this;
438 }
439
441 BSONObjBuilder& appendTimestamp( const StringData& fieldName , const Timestamp_t& ts = Timestamp_t() ) {
442 _b.appendNum( (char) Timestamp );
443 _b.appendStr( fieldName );
444
445 char buf[2 * sizeof(uint32_t)];
446 DataCursor cur(buf);
447 cur.writeLEAndAdvance<>(ts.increment());
448 cur.writeLEAndAdvance<>(ts.seconds());
449
450 _b.appendBuf(buf, sizeof(buf));
451 return *this;
452 }
453
454 BSONObjBuilder& append( const StringData& fieldName, const Timestamp_t& ts ) {
455 return appendTimestamp(fieldName, ts);
456 }
457
458 /*
459 Append an element of the deprecated DBRef type.
460 @deprecated
461 */
462 BSONObjBuilder& appendDBRef( const StringData& fieldName, const StringData& ns, const OID &oid ) {
463 _b.appendNum( (char) DBRef );
464 _b.appendStr( fieldName );
465 _b.appendNum( (int) ns.size() + 1 );
466 _b.appendStr( ns );
467 _b.appendBuf( oid.view().view(), OID::kOIDSize );
468 return *this;
469 }
470
471 BSONObjBuilder& append(const StringData& fieldName, const BSONDBRef& dbref) {
472 return appendDBRef(fieldName, dbref.ns, dbref.oid);
473 }
474
482 BSONObjBuilder& appendBinData( const StringData& fieldName, int len, BinDataType type, const void *data ) {
483 _b.appendNum( (char) BinData );
484 _b.appendStr( fieldName );
485 _b.appendNum( len );
486 _b.appendNum( (char) type );
487 _b.appendBuf( data, len );
488 return *this;
489 }
490
491 BSONObjBuilder& append(const StringData& fieldName, const BSONBinData& bd) {
492 return appendBinData(fieldName, bd.length, bd.type, bd.data);
493 }
494
501 BSONObjBuilder& appendBinDataArrayDeprecated( const char * fieldName , const void * data , int len ) {
502 _b.appendNum( (char) BinData );
503 _b.appendStr( fieldName );
504 _b.appendNum( len + 4 );
505 _b.appendNum( (char)0x2 );
506 _b.appendNum( len );
507 _b.appendBuf( data, len );
508 return *this;
509 }
510
514 BSONObjBuilder& appendCodeWScope( const StringData& fieldName, const StringData& code, const BSONObj &scope ) {
515 _b.appendNum( (char) CodeWScope );
516 _b.appendStr( fieldName );
517 _b.appendNum( ( int )( 4 + 4 + code.size() + 1 + scope.objsize() ) );
518 _b.appendNum( ( int ) code.size() + 1 );
519 _b.appendStr( code );
520 _b.appendBuf( ( void * )scope.objdata(), scope.objsize() );
521 return *this;
522 }
523
524 BSONObjBuilder& append(const StringData& fieldName, const BSONCodeWScope& cws) {
525 return appendCodeWScope(fieldName, cws.code, cws.scope);
526 }
527
528 void appendUndefined( const StringData& fieldName ) {
529 _b.appendNum( (char) Undefined );
530 _b.appendStr( fieldName );
531 }
532
533 /* helper function -- see Query::where() for primary way to do this. */
534 void appendWhere( const StringData& code, const BSONObj &scope ) {
535 appendCodeWScope( "$where" , code , scope );
536 }
537
541 void appendMinForType( const StringData& fieldName , int type );
542 void appendMaxForType( const StringData& fieldName , int type );
543
545 template < class T >
546 BSONObjBuilder& append( const StringData& fieldName, const std::vector< T >& vals );
547
548 template < class T >
549 BSONObjBuilder& append( const StringData& fieldName, const std::list< T >& vals );
550
552 template < class T >
553 BSONObjBuilder& append( const StringData& fieldName, const std::set< T >& vals );
554
559 template < class K, class T >
560 BSONObjBuilder& append( const StringData& fieldName, const std::map< K, T >& vals );
561
568 massert( 10335 , "builder does not own memory", owned() );
569 doneFast();
570 char* buf = _b.buf();
571 decouple();
572 return BSONObj::takeOwnership(buf);
573 }
574
581 return BSONObj(_done());
582 }
583
584 // Like 'done' above, but does not construct a BSONObj to return to the caller.
585 void doneFast() {
586 (void)_done();
587 }
588
594 BSONObj temp(_done());
595 _b.setlen(_b.len()-1); //next append should overwrite the EOO
596 _doneCalled = false;
597 return temp;
598 }
599
607 void abandon() {
608 _doneCalled = true;
609 }
610
611 void decouple() {
612 _b.decouple(); // post done() call version. be sure jsobj frees...
613 }
614
615 void appendKeys( const BSONObj& keyPattern , const BSONObj& values );
616
617 static std::string MONGO_CLIENT_FUNC numStr( int i ) {
618 if (i>=0 && i<100 && numStrsReady)
619 return numStrs[i];
620 StringBuilder o;
621 o << i;
622 return o.str();
623 }
624
626 BSONObjBuilderValueStream &operator<<( const StringData& name ) {
627 _s.endField( name );
628 return _s;
629 }
630
632 BSONObjBuilder& operator<<( GENOIDLabeler ) { return genOID(); }
633
634 Labeler operator<<( const Labeler::Label &l ) {
635 massert( 10336 , "No subobject started", _s.subobjStarted() );
636 return _s << l;
637 }
638
639 template<typename T>
640 BSONObjBuilderValueStream& operator<<( const BSONField<T>& f ) {
641 _s.endField( f.name() );
642 return _s;
643 }
644
645 template<typename T>
646 BSONObjBuilder& operator<<( const BSONFieldValue<T>& v ) {
647 append( v.name(), v.value() );
648 return *this;
649 }
650
651 BSONObjBuilder& operator<<( const BSONElement& e ){
652 append( e );
653 return *this;
654 }
655
656 bool isArray() const {
657 return false;
658 }
659
661 bool owned() const { return &_b == &_buf; }
662
663 BSONObjIterator iterator() const ;
664
665 bool hasField( const StringData& name ) const ;
666
667 int len() const { return _b.len(); }
668
669 BufBuilder& bb() { return _b; }
670
671 private:
672 char* _done() {
673 if ( _doneCalled )
674 return _b.buf() + _offset;
675
676 _doneCalled = true;
677 _s.endField();
678 _b.appendNum((char) EOO);
679 char *data = _b.buf() + _offset;
680 int size = _b.len() - _offset;
681 DataView(data).writeLE(size);
682 if ( _tracker )
683 _tracker->got( size );
684 return data;
685 }
686
687 BufBuilder &_b;
688 BufBuilder _buf;
689 int _offset;
690 BSONObjBuilderValueStream _s;
691 BSONSizeTracker * _tracker;
692 bool _doneCalled;
693
694 static const std::string numStrs[100]; // cache of 0 to 99 inclusive
695 static bool numStrsReady; // for static init safety.
696 };
697
698 class BSONArrayBuilder : boost::noncopyable {
699 public:
700 BSONArrayBuilder() : _i(0), _b() {}
701 BSONArrayBuilder( BufBuilder &_b ) : _i(0), _b(_b) {}
702 BSONArrayBuilder( int initialSize ) : _i(0), _b(initialSize) {}
703
704 template <typename T>
705 BSONArrayBuilder& append(const T& x) {
706 _b.append(num(), x);
707 return *this;
708 }
709
710 BSONArrayBuilder& append(const BSONElement& e) {
711 _b.appendAs(e, num());
712 return *this;
713 }
714
715 BSONArrayBuilder& operator<<(const BSONElement& e) {
716 return append(e);
717 }
718
719 template <typename T>
720 BSONArrayBuilder& operator<<(const T& x) {
721 _b << num().c_str() << x;
722 return *this;
723 }
724
725 void appendNull() {
726 _b.appendNull(num());
727 }
728
729 void appendUndefined() {
730 _b.appendUndefined(num());
731 }
732
737 BSONArray arr() { return BSONArray(_b.obj()); }
738 BSONObj obj() { return _b.obj(); }
739
740 BSONObj done() { return _b.done(); }
741
742 void doneFast() { _b.doneFast(); }
743
744 template < class T >
745 BSONArrayBuilder& append( const std::list< T >& vals );
746
747 template < class T >
748 BSONArrayBuilder& append( const std::set< T >& vals );
749
750 // These two just use next position
751 BufBuilder &subobjStart() { return _b.subobjStart( num() ); }
752 BufBuilder &subarrayStart() { return _b.subarrayStart( num() ); }
753
754 BSONArrayBuilder& appendRegex(const StringData& regex, const StringData& options = "") {
755 _b.appendRegex(num(), regex, options);
756 return *this;
757 }
758
759 BSONArrayBuilder& appendBinData(int len, BinDataType type, const void* data) {
760 _b.appendBinData(num(), len, type, data);
761 return *this;
762 }
763
764 BSONArrayBuilder& appendCode(const StringData& code) {
765 _b.appendCode(num(), code);
766 return *this;
767 }
768
769 BSONArrayBuilder& appendCodeWScope(const StringData& code, const BSONObj& scope) {
770 _b.appendCodeWScope(num(), code, scope);
771 return *this;
772 }
773
774 BSONArrayBuilder& appendTimeT(time_t dt) {
775 _b.appendTimeT(num(), dt);
776 return *this;
777 }
778
779 BSONArrayBuilder& appendDate(Date_t dt) {
780 _b.appendDate(num(), dt);
781 return *this;
782 }
783
784 BSONArrayBuilder& appendBool(bool val) {
785 _b.appendBool(num(), val);
786 return *this;
787 }
788
789 bool isArray() const {
790 return true;
791 }
792
793 int len() const { return _b.len(); }
794 int arrSize() const { return _i; }
795
796 BufBuilder& bb() { return _b.bb(); }
797
798 private:
799
800 std::string num() { return _b.numStr(_i++); }
801 int _i;
802 BSONObjBuilder _b;
803 };
804
805 template < class T >
806 inline BSONObjBuilder& BSONObjBuilder::append( const StringData& fieldName, const std::vector< T >& vals ) {
807 BSONObjBuilder arrBuilder;
808 for ( unsigned int i = 0; i < vals.size(); ++i )
809 arrBuilder.append( numStr( i ), vals[ i ] );
810 appendArray( fieldName, arrBuilder.done() );
811 return *this;
812 }
813
814 template < class L >
815 inline BSONObjBuilder& _appendIt( BSONObjBuilder& _this, const StringData& fieldName, const L& vals ) {
816 BSONObjBuilder arrBuilder;
817 int n = 0;
818 for( typename L::const_iterator i = vals.begin(); i != vals.end(); i++ )
819 arrBuilder.append( BSONObjBuilder::numStr(n++), *i );
820 _this.appendArray( fieldName, arrBuilder.done() );
821 return _this;
822 }
823
824 template < class T >
825 inline BSONObjBuilder& BSONObjBuilder::append( const StringData& fieldName, const std::list< T >& vals ) {
826 return _appendIt< std::list< T > >( *this, fieldName, vals );
827 }
828
829 template < class T >
830 inline BSONObjBuilder& BSONObjBuilder::append( const StringData& fieldName, const std::set< T >& vals ) {
831 return _appendIt< std::set< T > >( *this, fieldName, vals );
832 }
833
834 template < class K, class T >
835 inline BSONObjBuilder& BSONObjBuilder::append( const StringData& fieldName, const std::map< K, T >& vals ) {
836 BSONObjBuilder bob;
837 for( typename std::map<K,T>::const_iterator i = vals.begin(); i != vals.end(); ++i ){
838 bob.append(i->first, i->second);
839 }
840 append(fieldName, bob.obj());
841 return *this;
842 }
843
844
845 template < class L >
846 inline BSONArrayBuilder& _appendArrayIt( BSONArrayBuilder& _this, const L& vals ) {
847 for( typename L::const_iterator i = vals.begin(); i != vals.end(); i++ )
848 _this.append( *i );
849 return _this;
850 }
851
852 template < class T >
853 inline BSONArrayBuilder& BSONArrayBuilder::append( const std::list< T >& vals ) {
854 return _appendArrayIt< std::list< T > >( *this, vals );
855 }
856
857 template < class T >
858 inline BSONArrayBuilder& BSONArrayBuilder::append( const std::set< T >& vals ) {
859 return _appendArrayIt< std::set< T > >( *this, vals );
860 }
861
862 template<typename T>
863 inline BSONFieldValue<BSONObj> BSONField<T>::query( const char * q , const T& t ) const {
864 BSONObjBuilder b;
865 b.append( q , t );
866 return BSONFieldValue<BSONObj>( _name , b.obj() );
867 }
868
869 // $or helper: OR(BSON("x" << GT << 7), BSON("y" << LT 6));
870 inline BSONObj OR(const BSONObj& a, const BSONObj& b)
871 { return BSON( "$or" << BSON_ARRAY(a << b) ); }
872 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c)
873 { return BSON( "$or" << BSON_ARRAY(a << b << c) ); }
874 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d)
875 { return BSON( "$or" << BSON_ARRAY(a << b << c << d) ); }
876 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e)
877 { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e) ); }
878 inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f)
879 { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e << f) ); }
880
881}
Definition bsonobjbuilder.h:698
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition bsonobjbuilder.h:737
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
int valuesize() const
size in bytes of the element's value (when applicable).
Definition bsonelement.h:166
const char * value() const
raw data of the element's value (so be careful).
Definition bsonelement.h:162
int size(int maxLen) const
Size of the element.
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
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:109
Definition bsonmisc.h:197
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:49
BSONObjBuilder(BufBuilder &baseBuilder)
Definition bsonobjbuilder.h:68
void abandon()
Make it look as if "done" has been called, so that our destructor is a no-op.
Definition bsonobjbuilder.h:607
BSONObjBuilder & append(const StringData &fieldName, const std::string &str)
Append a string element.
Definition bsonobjbuilder.h:391
bool owned() const
Definition bsonobjbuilder.h:661
BSONObjBuilder & append(const StringData &fieldName, BSONObj subObj)
add a subobject as a member
Definition bsonobjbuilder.h:125
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition bsonobjbuilder.h:109
BSONObjBuilder & appendElements(BSONObj x)
add all the fields from the object specified to this object
BSONObjBuilder & append(const StringData &fieldName, OID oid)
Append a BSON Object ID.
Definition bsonobjbuilder.h:315
BSONObjBuilder & append(const StringData &fieldName, int n)
Append a 32 bit integer element.
Definition bsonobjbuilder.h:202
BSONObjBuilder & append(const StringData &fieldName, double n)
Append a double element.
Definition bsonobjbuilder.h:278
BSONObjBuilder & append(const StringData &fieldName, long long n)
Append a NumberLong.
Definition bsonobjbuilder.h:215
BSONObj asTempObj()
Peek at what is in the builder, but leave the builder ready for more appends.
Definition bsonobjbuilder.h:593
BSONObjBuilder & appendOID(const StringData &fieldName, OID *oid=0, bool generateIfBlank=false)
Append a BSON Object ID (OID type).
Definition bsonobjbuilder.h:294
void appendNull()
Implements builder interface but no-op in ObjBuilder.
Definition bsonobjbuilder.h:416
BSONObjBuilder & append(const StringData &fieldName, unsigned n)
Append a 32 bit unsigned element - cast to a signed int.
Definition bsonobjbuilder.h:210
BSONObjBuilder & appendObject(const StringData &fieldName, const char *objdata, int size=0)
add a subobject as a member
Definition bsonobjbuilder.h:133
BSONObjBuilder & appendNumber(const StringData &fieldName, int n)
appendNumber is a series of method for appending the smallest sensible type mostly for JS
Definition bsonobjbuilder.h:240
BSONObjBuilder & appendDate(const StringData &fieldName, Date_t dt)
Append a date.
BSONObjBuilder & append(const StringData &fieldName, bool val)
Append a boolean element.
Definition bsonobjbuilder.h:194
BSONObjBuilder & appendCodeWScope(const StringData &fieldName, const StringData &code, const BSONObj &scope)
Append to the BSON object a field of type CodeWScope.
Definition bsonobjbuilder.h:514
BSONObjBuilder & append(const StringData &fieldName, const char *str, int sz)
Append a string element.
Definition bsonobjbuilder.h:379
BSONObjBuilder & append(const StringData &fieldName, const char *str)
Append a string element.
Definition bsonobjbuilder.h:387
BSONObjBuilder & appendNull(const StringData &fieldName)
Append a Null element to the object.
Definition bsonobjbuilder.h:421
BSONObjBuilder & appendTimestamp(const StringData &fieldName, const Timestamp_t &ts=Timestamp_t())
Append a Timestamp element to the object.
Definition bsonobjbuilder.h:441
BSONObjBuilder & appendElementsUnique(BSONObj x)
add all the fields from the object specified to this object if they don't exist already
BufBuilder & subarrayStart(const StringData &fieldName)
add header for a new subarray and return bufbuilder for writing to the subarray's body
Definition bsonobjbuilder.h:179
BSONObjBuilder & append(const StringData &fieldName, const StringData &str)
Append a string element.
Definition bsonobjbuilder.h:395
BSONObjBuilderValueStream & operator<<(const StringData &name)
Stream oriented way to add field names and values.
Definition bsonobjbuilder.h:626
BSONObjBuilder & appendAs(const BSONElement &e, const StringData &fieldName)
append an element but with a new name
Definition bsonobjbuilder.h:116
bool appendAsNumber(const StringData &fieldName, const std::string &data)
tries to append the data as a number
BSONObjBuilder & appendBinDataArrayDeprecated(const char *fieldName, const void *data, int len)
Subtype 2 is deprecated.
Definition bsonobjbuilder.h:501
BSONObjBuilder & appendBinData(const StringData &fieldName, int len, BinDataType type, const void *data)
Append a binary data element.
Definition bsonobjbuilder.h:482
BSONObjBuilder & appendBool(const StringData &fieldName, int val)
Append a boolean element.
Definition bsonobjbuilder.h:186
BSONObjBuilder & appendRegex(const StringData &fieldName, const StringData &regex, const StringData &options="")
Append a regular expression value.
Definition bsonobjbuilder.h:353
BSONObj obj()
destructive The returned BSONObj will free the buffer when it is finished.
Definition bsonobjbuilder.h:567
BSONObj done()
Fetch the object we have built.
Definition bsonobjbuilder.h:580
BSONObjBuilder & appendTimeT(const StringData &fieldName, time_t dt)
Append a time_t date.
Definition bsonobjbuilder.h:334
BufBuilder & subobjStart(const StringData &fieldName)
add header for a new subobject and return bufbuilder for writing to the subobject's body
Definition bsonobjbuilder.h:158
BSONObjBuilder & appendArray(const StringData &fieldName, const BSONObj &subObj)
add a subobject as a member with type Array.
Definition bsonobjbuilder.h:167
BSONObjBuilder & genOID()
Generate and assign an object id for the _id field.
Definition bsonobjbuilder.h:326
void appendMinForType(const StringData &fieldName, int type)
these are the min/max when comparing, not strict min/max elements for a given type
BSONObjBuilder & operator<<(GENOIDLabeler)
Stream oriented way to add field names and values.
Definition bsonobjbuilder.h:632
BSONObjBuilder(int initsize=512)
Definition bsonobjbuilder.h:52
BSONObjBuilder & appendIntOrLL(const StringData &fieldName, long long n)
appends a number.
Definition bsonobjbuilder.h:223
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
int objsize() const
Definition bsonobj.h:304
const char * objdata() const
Definition bsonobj.h:299
used in conjuction with BSONObjBuilder, allows for proper buffer size to prevent crazy memory usage
Definition bsonmisc.h:240
Definition bsonmisc.h:116
Object ID type.
Definition oid.h:60
void clear()
initialize to 'null'
Definition oid.h:92
void init()
sets the contents to a new oid / randomized value
Definition shared_buffer.h:67
Definition timestamp.h:23
int len() const
Definition builder.h:199
the main MongoDB namespace
Definition bulk_operation_builder.h:24
MONGO_CLIENT_API bool isArray(const StringData &str)
Tests whether the JSON string is an Array.
@ 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
@ 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 bsonobj.h:559
Definition bsonmisc.h:164
Definition bsonmisc.h:150
Definition bsonmisc.h:157
Definition time_support.h:39
Definition bsonmisc.h:82
Definition bsonmisc.h:118