diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-02-08 17:55:17 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-02-08 17:55:17 -0500 |
commit | 595328d2aff14ab8b279d12cf8132edacf273c0e (patch) | |
tree | 9b1008eaa96ca6224f2043507f0297ed29622a77 /client | |
parent | 111528c3f6bd646931dc04ceb6185c6cf4d75e7f (diff) | |
download | mongo-595328d2aff14ab8b279d12cf8132edacf273c0e.tar.gz |
Model::save()
Diffstat (limited to 'client')
-rw-r--r-- | client/model.cpp | 34 | ||||
-rw-r--r-- | client/model.h | 4 |
2 files changed, 36 insertions, 2 deletions
diff --git a/client/model.cpp b/client/model.cpp index f8a5e596f40..d740b44f574 100644 --- a/client/model.cpp +++ b/client/model.cpp @@ -22,7 +22,7 @@ namespace mongo { - bool Model::load(BSONObj& query) { + bool Model::load(BSONObj& query){ ScopedDbConnection scoped( modelServer() ); DBClientWithCommands& conn = scoped.conn(); BSONObj b = conn.findOne(getNS(), query); @@ -30,9 +30,39 @@ namespace mongo { if ( b.isEmpty() ) return false; - + unserialize(b); + _id = b["_id"]; return true; } + void Model::save(){ + ScopedDbConnection scoped( modelServer() ); + DBClientWithCommands& conn = scoped.conn(); + + BSONObjBuilder b; + serialize( b ); + + if ( _id.eoo() ){ + OID oid; + b.appendOID( "_id" , &oid ); + + BSONObj o = b.doneAndDecouple(); + conn.insert( getNS() , o ); + _id = o["_id"]; + + log(4) << "inserted new model" << endl; + } + else { + b.append( _id ); + BSONObjBuilder id; + id.append( _id ); + conn.update( getNS() , id.doneAndDecouple() , b.doneAndDecouple() ); + + log(4) << "updated old model" << endl; + } + + scoped.done(); + } + } // namespace mongo diff --git a/client/model.h b/client/model.h index b46deb01855..3b565701ff9 100644 --- a/client/model.h +++ b/client/model.h @@ -55,6 +55,10 @@ namespace mongo { @return true if successful. */ bool load(BSONObj& query); + void save(); + + private: + BSONElement _id; }; } // namespace mongo |