Re: [Newbie] C++ Model tutorial...

299 views
Skip to first unread message

Adam C

unread,
Sep 3, 2012, 12:28:58 PM9/3/12
to mongod...@googlegroups.com
From the docs:

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


If you are looking for higher level modeling (data structures etc.) the C++ driver won't have anything like that.

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

PlugGulp

unread,
Sep 3, 2012, 6:43:20 PM9/3/12
to mongod...@googlegroups.com
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 &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


William Z

unread,
Sep 5, 2012, 2:16:55 PM9/5/12
to mongod...@googlegroups.com

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

PlugGulp

unread,
Sep 5, 2012, 4:56:55 PM9/5/12
to mongod...@googlegroups.com
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
main_t.cpp

William Zola

unread,
Oct 10, 2012, 1:29:46 PM10/10/12
to mongod...@googlegroups.com
Hi Plug!

Unfortunately, I'm not a good enough C++ programmer to review your code.  You may, however, find this SERVER ticket relevant:  https://jira.mongodb.org/browse/SERVER-7314

 -William
Reply all
Reply to author
Forward
0 new messages