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.
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 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<http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj_b...>&to); // Implemented by this class. Pumps this class's member variables into the BOB. virtual void unserialize(const BSONObj<http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj.html>&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.
> 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.
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.
> 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<http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj_b...>&to); // Implemented by this class. Pumps this class's member variables > into the BOB. > virtual void unserialize(const BSONObj<http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj.html>&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.
Thank you for the reply. Yesterday I wrote a small piece of code to understand the usage of Model class. It is actually similar to what you described in your post. I am attaching the source file here. Please could you review and comment on the code. Is that the correct way of using Model class?
On Wednesday, 5 September 2012 19:16:55 UTC+1, William Z wrote:
> 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"
>> 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<http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj_b...>&to); // Implemented by this class. Pumps this class's member variables >> into the BOB. >> virtual void unserialize(const BSONObj<http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj.html>&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.
On Wednesday, September 5, 2012 1:56:55 PM UTC-7, PlugGulp wrote:
> Hello William,
> Thank you for the reply. Yesterday I wrote a small piece of code to > understand the usage of Model class. It is actually similar to what you > described in your post. I am attaching the source file here. Please could > you review and comment on the code. Is that the correct way of using Model > class?
> Thanks and regards,
> ~Plug
> On Wednesday, 5 September 2012 19:16:55 UTC+1, William Z wrote:
>> 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.