开发者

Trying to persist objects with MongoDB

开发者 https://www.devze.com 2023-01-23 07:56 出处:网络
First things first. I may be completely off track with this. I\'m still learning with Mongo and NOSql solutions in general. We have a new component to our app we are writing and instead of sitting dow

First things first. I may be completely off track with this. I'm still learning with Mongo and NOSql solutions in general. We have a new component to our app we are writing and instead of sitting down and messing with a relation database we wanted to use something that would fit better with our o开发者_JAVA技巧bjects.

So let's take a simple example:

Class User extends \Model {
    public $name;
    public $hobbies;
}

$name would just be a string. But lets say $hobbies is an object or an array of objects. I want to just be able to throw this into a data store and be able to retrieve it later.

At first I went down the road of breaking the object down into an array and storing that in Mongo and then pulling it back out and populating an object. Pretty simple with a generic import and export method I made. The problem comes when I have some robust objects that have other objects as member variables and so on. At that point I could still export into a multidimensional array and store it fine. But importing back into the objects became problematic.

The other option I could do is just seralize() the object and store that in mongo along with some descriptive data.

Sooooo. Thoughts?

Part of my problem here is that I'm new to the NOSql products and not sure their full limitations/potential. Am I just looking at Mongo wrong and trying to make it do something it's not meant to do? I'd prefer not to use some 3rd party module and would rather write something simple and lightweight.


Although I didn't want to use a 3rd party app, Doctrine's ODM for Mongo seems to do exactly what I wanted. Got it set up and seems to be working good so far.

http://www.doctrine-project.org/projects/mongodb-odm.html


I think serialize is the way to go here. Then you can use the magic methods __sleep and __wakeup for each class to handle any tricky situations.


The other option here to serialize your objects into arrays instead of just using "serialize". If I'm not mistaken, you can actually override the "serialize" method in these sub-objects and basically have them serialize themselves into arrays (or more specifically hash-tables).

If Doctrine does this for you, then all the better. But if you just want this feature you can probably cook your own.

0

精彩评论

暂无评论...
验证码 换一张
取 消