Received: by 10.66.83.40 with SMTP id n8mr3527462pay.42.1346869029830; Wed, 05 Sep 2012 11:17:09 -0700 (PDT) X-BeenThere: mongodb-user@googlegroups.com Received: by 10.68.195.39 with SMTP id ib7ls3076294pbc.9.gmail; Wed, 05 Sep 2012 11:16:56 -0700 (PDT) Received: by 10.68.244.73 with SMTP id xe9mr4291275pbc.10.1346869016685; Wed, 05 Sep 2012 11:16:56 -0700 (PDT) Date: Wed, 5 Sep 2012 11:16:55 -0700 (PDT) From: William Z To: mongodb-user@googlegroups.com Message-Id: <25e30eb6-ff36-4cb2-8748-b0a48c72252c@googlegroups.com> In-Reply-To: References: <4ed9eaa4-d4e6-477a-93fc-bb494b198637@googlegroups.com> <72c0841c-bcf6-44ec-9f6b-4a38ee576084@googlegroups.com> Subject: Re: [Newbie] C++ Model tutorial... MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_303_19220591.1346869015658" ------=_Part_303_19220591.1346869015658 Content-Type: multipart/alternative; boundary="----=_Part_304_11968040.1346869015658" ------=_Part_304_11968040.1346869015658 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit The "Model" class is an optional Object to Document mapper for C++. You would use it if you wanted to extend and existing class to have built-in methods to save itself from MongoDB and to load itself from MongoDB. When you implemented your inherited class, you'd write the 'serialize()' and 'unserialize()' methods to use BSONObjBuilder to build a BSON representation of this class. Once you have an instance of the class, you can call the 'load()' method to instantiate the class from the values stored in MongoDB, and call the 'save()' method to save the class values into MongoDB for later use. Use of the "Model" class is definitely optional, and not recommended for beginners. Unfortunately, I could not find any further documentation on the use of this class, nor could I find any tutorial examples. Let me know if you have further questions. -William On Monday, September 3, 2012 3:43:20 PM UTC-7, PlugGulp wrote: > > On Monday, September 3, 2012 5:28:58 PM UTC+1, Adam C wrote: >> >> From the docs: >> >> "Model is a base class for defining objects which are serializable to the >> Mongo database via the database driver" >> >> http://api.mongodb.org/cplusplus/2.2.0/classmongo_1_1_model.html#details >> >> > Thanks for the reply. I completely understand that the Model class is for > serialization and deserialization. But my question was related to how it is > used, when (i.e. during what circumstances) and why it is used when there > is already a BSONObjBuilder. > > >> If you are looking for higher level modeling (data structures etc.) the >> C++ driver won't have anything like that. >> >> > I think thats the reason the Model class is provided. But I am not sure > about that. For e.g. say I define a class as such: > > class MyModel : public mongo::Model > { > public: > virtual const char* getNS(); // Implemented by this class. > virtual void serialize(BSONObjBuilder&to); // Implemented by this class. Pumps this class's member variables > into the BOB. > virtual void unserialize(const BSONObj&from); // Implemented by this class. Extracts data from the BOB and stores > into this class's member variables. > virtual string modelServer(); // Implemented by this class. > > private: > // Member variables representing data of this model. > }; > > Now, once I implement the MyModel class, how do I use it? And when and why > should I use it? There is no example/document/tutorial explaining that. > > Thanks and regards, > > ~Plug > > > Adam >> >> >> On Monday, September 3, 2012 3:31:27 AM UTC+1, PlugGulp wrote: >>> >>> Hi, >>> >>> I am new to MongoDB. I was browsing the code of C++ driver and found the >>> class Model. How to use this class and when and why to use it? Please could >>> you provide example or point me to any tutorial that will explain that. >>> >>> Thanks and regards, >>> >>> ~Plug >>> >>> ------=_Part_304_11968040.1346869015658 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
The "Model" class is an optional Object to Document mapper for C++.&nbs= p; You would use it if you wanted to extend and existing class to have buil= t-in methods to save itself from MongoDB and to load itself from MongoDB.
When you implemented your inherited class, you'd write the 'serialize= ()' and 'unserialize()' methods to use BSONObjBuilder to build a BSON repre= sentation of this class. 

Once you have an instance of the cla= ss, you can call the 'load()' method to instantiate the class from the valu= es stored in MongoDB, and call the 'save()' method to save the class values= into MongoDB for later use.

Use of the "Model" class is definitely = optional, and not recommended for beginners.  Unfortunately, I could n= ot find any further documentation on the use of this class, nor could I fin= d any tutorial examples.

Let me know if you have further questions.<= br>
 -William



On Monday, September 3, 2012 3:43:20 P= M UTC-7, PlugGulp wrote:
On Mon= day, September 3, 2012 5:28:58 PM UTC+1, Adam C wrote:
From the docs:

"Model is a base class= for defining objects which are serializable to the Mongo database via the = database driver"



Thanks for the reply. = I completely understand that the Model class is for serialization and deser= ialization. But my question was related to how it is used, when (i.e. durin= g what circumstances) and why it is used when there is already a BSONObjBui= lder.
 
If you are looking for higher level modeling (data structures etc.) = the C++ driver won't have anything like that.


I think thats the reason the Model class is provided. Bu= t I am not sure about that. For e.g. say I define a class as such:

c= lass MyModel : public mongo::Model
{
     public:=
         virtual const char* ge= tNS(); // Implemented by this class.
      = ;   virtual void serialize(BS= ONObjBuilder &to); // Implemented by this class. Pumps this class's= member variables into the BOB.
      &nbs= p;  virtual void unserialize(const BSONObj= &from); // Implemented by this class. Extracts data from the BOB a= nd stores into this class's member variables.
    &n= bsp;    virtual string modelServer(); // Implemented by thi= s class.

     private:
   &nbs= p;     // Member variables representing data of this model.
};=

Now, once I implement the MyModel class, how do I use it? And when = and why should I use it? There is no example/document/tutorial explaining t= hat.

Thanks and regards,

~Plug


Adam


On M= onday, September 3, 2012 3:31:27 AM UTC+1, PlugGulp wrote:
Hi,

I am new to MongoDB. I was browsing the c= ode of C++ driver and found the class Model. How to use this class and when= and why to use it? Please could you provide example or point me to any tut= orial that will explain that.

Thanks and regards,

~Plug
------=_Part_304_11968040.1346869015658-- ------=_Part_303_19220591.1346869015658--