Permalink
Browse files

$or helper in C++ SERVER-1332

  • Loading branch information...
1 parent 0e2409c commit 8047e6afb2722a1daca76ab37d883325bde5a0c2 @RedBeard0531 RedBeard0531 committed Jul 19, 2010
Showing with 42 additions and 0 deletions.
  1. +10 −0 bson/bsonmisc.h
  2. +12 −0 bson/bsonobjbuilder.h
  3. +20 −0 dbtests/jsobjtests.cpp
View
10 bson/bsonmisc.h
@@ -116,6 +116,16 @@ namespace mongo {
extern Labeler::Label LTE;
extern Labeler::Label NE;
extern Labeler::Label SIZE;
+
+
+ // $or helper: OR(BSON("x" << GT << 7), BSON("y" << LT << 6));
+ // becomes : {$or: [{x: {$gt: 7}}, {y: {$lt: 6}}]}
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b);
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c);
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d);
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e);
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f);
+ // definitions in bsonobjbuilder.h b/c of incomplete types
// Utility class to implement BSON( key << val ) as described above.
class BSONObjBuilderValueStream : public boost::noncopyable {
View
12 bson/bsonobjbuilder.h
@@ -728,5 +728,17 @@ namespace mongo {
appendArray( fieldName, arrBuilder.done() );
return *this;
}
+
+ // $or helper: OR(BSON("x" << GT << 7), BSON("y" << LT 6));
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b)
+ { return BSON( "$or" << BSON_ARRAY(a << b) ); }
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c)
+ { return BSON( "$or" << BSON_ARRAY(a << b << c) ); }
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d)
+ { return BSON( "$or" << BSON_ARRAY(a << b << c << d) ); }
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e)
+ { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e) ); }
+ inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f)
+ { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e << f) ); }
}
View
20 dbtests/jsobjtests.cpp
@@ -869,6 +869,19 @@ namespace JsobjTests {
<< "x" << "p" );
}
};
+ class LabelishOr : public LabelBase {
+ BSONObj expected() {
+ return BSON( "$or" << BSON_ARRAY(
+ BSON("a" << BSON( "$gt" << 1 << "$lte" << "x" ))
+ << BSON("b" << BSON( "$ne" << 1 << "$ne" << "f" << "$ne" << 22.3 ))
+ << BSON("x" << "p" )));
+ }
+ BSONObj actual() {
+ return OR( BSON( "a" << GT << 1 << LTE << "x"),
+ BSON( "b" << NE << 1 << NE << "f" << NE << 22.3),
+ BSON( "x" << "p" ) );
+ }
+ };
class Unallowed {
public:
@@ -1660,6 +1673,13 @@ namespace JsobjTests {
add< ValueStreamTests::LabelDoubleShares >();
add< ValueStreamTests::LabelSize >();
add< ValueStreamTests::LabelMulti >();
+ add< ValueStreamTests::LabelishOr >();
+ add< ValueStreamTests::Unallowed >();
+ add< ValueStreamTests::ElementAppend >();
+ add< SubObjectBuilder >();
+ add< DateBuilder >();
+ add< DateNowBuilder >();
+ add< TimeTBuilder >();
add< ValueStreamTests::Unallowed >();
add< ValueStreamTests::ElementAppend >();
add< SubObjectBuilder >();

0 comments on commit 8047e6a

Please sign in to comment.