monkeyUser
monkeyUser

Reputation: 4679

Denormalization Data in MongoDb Doctrine Symfony 2

I'm Following this Doc

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/tutorials/getting-started.html

And

http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

When I Save My Document, I have two Collection

like this:

   {
    "_id" : ObjectId("5458e370d16fb63f250041a7"),
    "name" : "A Foo Bar",
    "price" : 19.99,
    "posts" : [ 
        {
            "$ref" : "Embedd",
            "$id" : ObjectId("5458e370d16fb63f250041a8"),
            "$db" : "test_database"
        }
    ]
}

I'd like have

   {
    "_id" : ObjectId("5458e370d16fb63f250041a7"),
    "name" : "A Foo Bar",
    "price" : 19.99,
    "posts" : [ 
        {
           "mycomment" :"dsdsds"
           " date" : date
        }
    ]
}

I want denormalization my data. How Can i Do it?

Can I use Methods like $push,$addToSet etc of mongoDb?

Thanks

Upvotes: 3

Views: 469

Answers (1)

jmikola
jmikola

Reputation: 6922

Doctrine ODM supports both references and embedded documents.

In your first example, you're using references. The main document (let's assume it's called Product) references many Post documents. Those Post documents live in their own collection (for some reason this is named Embedd -- I would suggest renaming that if you keep this schema). By default, ODM uses the DBRef convention for references, so each reference is itself a small embedded document with $ref, $id, and $db fields.

Denormalization can be achieved by using embedded documents (an @EmbedMany mapping in your case). If you were embedding a Post document, the Post class should be mapped as an @EmbeddedDocument. This tells ODM that it's not a first-class document (belonging to its own collection), so it won't have to worry about tracking it by _id and the like (in fact, embedded documents won't even need identifiers unless you want to map one).

My rule of thumb for deciding to embed or references has generally been asking myself, "Will I need this document outside of the context of the parent document?" If a Post will not have an identity outside of the Product record, I'm comfortable embedding it; however, if I find later that my application also wants to show users a list of all of their Posts, or that I need to query by Posts (e.g. a feed of all recent Posts, irrespective of Product), then I may want to reference documents in a Posts collection (or simply duplicate embedded Posts as needed).

Alternatively, you may decide that Posts should exist in both their own collection and be embedded on Product. In that case, you can create an AbstractPost class as a @MappedSuperclass and define common fields there. Then, extend this with both Post and EmbeddedPost sub-classes (mapped accordingly). You'll be responsible for creating some code to generate an EmbeddedPost from a Post document, which will be suitable for embedding in the Product.posts array. Furthermore, you'll need to handle data synchronization between the top-level and embedded Posts (e.g. if someone edits a Post comment, you may want all the corresponding embedded versions updated as well).


On the subject of references: ODM also supports a simple option for reference mappings, in which case it will just store the referenced document's _id instead of the larger DBRef object. In most cases, having DBRef store the collection and database name for each referenced document is quite redundant; however, DBRef is actually useful if you're using single-collection inheritance, as ODM uses the object to store extra discriminator information (i.e. the class of the referenced object).

Upvotes: 7

Related Questions