summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwight <dmerriman@gmail.com>2009-09-11 10:58:59 -0400
committerDwight <dmerriman@gmail.com>2009-09-11 10:58:59 -0400
commit1bdc3762c60f6ea73b2cbfedbf94851b75b40603 (patch)
tree48065aed591205785d2e1c02bfaf145524538ff7
parent47a7238303990fbd9ebf8834d8abd2b7fc50fe72 (diff)
downloadmongo-1bdc3762c60f6ea73b2cbfedbf94851b75b40603.tar.gz
GENOID feature for BSON in c++
BSON( GENOID << z << 3 );
-rw-r--r--db/db.sln1
-rw-r--r--db/jsobj.cpp9
-rw-r--r--db/jsobj.h14
3 files changed, 23 insertions, 1 deletions
diff --git a/db/db.sln b/db/db.sln
index 4607374eb9f..5607cd8bd65 100644
--- a/db/db.sln
+++ b/db/db.sln
@@ -27,7 +27,6 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{2B262D59-9DC7-4BF1-A431-1BD4966899A5}"
ProjectSection(SolutionItems) = preProject
..\tools\dump.cpp = ..\tools\dump.cpp
- ..\tools\export.cpp = ..\tools\export.cpp
..\tools\importJSON.cpp = ..\tools\importJSON.cpp
EndProjectSection
EndProject
diff --git a/db/jsobj.cpp b/db/jsobj.cpp
index 78951cc772e..a988167bc9b 100644
--- a/db/jsobj.cpp
+++ b/db/jsobj.cpp
@@ -37,6 +37,14 @@ namespace mongo {
return s;
}
+ IDLabeler GENOID;
+ BSONObjBuilder& operator<<(BSONObjBuilder& b, IDLabeler& id) {
+ OID oid;
+ oid.init();
+ b.appendOID("_id", &oid);
+ return b;
+ }
+
string BSONElement::toString( bool includeFieldName ) const {
stringstream s;
if ( includeFieldName && type() != EOO )
@@ -1172,6 +1180,7 @@ namespace mongo {
struct BsonUnitTest : public UnitTest {
void testRegex() {
+
BSONObjBuilder b;
b.appendRegex("x", "foo");
BSONObj o = b.done();
diff --git a/db/jsobj.h b/db/jsobj.h
index 54a278c306f..4e1f3ac8213 100644
--- a/db/jsobj.h
+++ b/db/jsobj.h
@@ -977,16 +977,30 @@ namespace mongo {
typedef set< BSONObj, BSONObjCmpDefaultOrder > BSONObjSetDefaultOrder;
/** Use BSON macro to build a BSONObj from a stream
+
e.g.,
BSON( "name" << "joe" << "age" << 33 )
+
+ with auto-generated object id:
+ BSON( GENOID << "name" << "joe" << "age" << 33 )
The labels GT, GTE, LT, LTE, NE can be helpful for stream-oriented construction
of a BSONObj, particularly when assembling a Query. For example,
BSON( "a" << GT << 23.4 << NE << 30 << "b" << 2 ) produces the object
{ a: { \$gt: 23.4, \$ne: 30 }, b: 2 }.
+
+ Use BSONwithID to create a BSON object with an object id (_id) auto prepended.
*/
#define BSON(x) (( mongo::BSONObjBuilder() << x ).obj())
+
+ /* Utility class to auto assign object IDs.
+ Example:
+ cout << BSON( GENOID << z << 3 ); // { _id : ..., z : 3 }
+ */
+ extern struct IDLabeler { } GENOID;
+ BSONObjBuilder& operator<<(BSONObjBuilder& b, IDLabeler& id);
+
// Utility class to implement GT, GTE, etc as described above.
class Labeler {
public: