mikechabot / quickrant-web

Express yourself without yourself
0 stars 0 forks source link

Simplify the mongo schema #11

Open mikechabot opened 9 years ago

mikechabot commented 9 years ago

Springs MongoRepository is great for lots of things, but since its so generic it lacks the ability to easily construct complex queries on complex objects, such as aggregating the total number of rants with a given emotion or question, which lives in a Selection object within the Rant. As a result, its better to simplify the Rant document object in order to utilize the power of MongoRepository, such as rantRepository.findByEmotion(String emotion), rather than implementing annotated queries, with mongos query language.

http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mongo.repositories.html

mikechabot commented 9 years ago

Mongo supports updating with JS, which is badass. Here's a script that'll move the Selection and Ranter objects to the top level Rant object. Server model have been updated to reflect this, now just need to update UI:

db.getCollection('rant').find({}).forEach(
    function(doc) {

        var selection = doc.selection;
        if (selection) {
            doc.emotion = doc.selection.emotion;
            doc.question = doc.selection.question;
            delete doc.selection;
        }

        var ranter = doc.ranter;
        if (ranter) {
            doc.name = doc.ranter.name;
            doc.location = doc.ranter.location;
            delete doc.ranter;
        }

        var comments = doc.comments;
        if (comments && comments.length > 0) {
            comments.forEach(function(comment) {
                var text = comment.comment;
                if (text) {
                    comment.text = comment.comment;
                    delete comment.comment;
                }
            });            
        }

        var text = doc.rant;
        if (text) {
            doc.text = text;
            delete doc.rant;
        }

        db.getCollection('rant').save(doc);
   }
);
mikechabot commented 9 years ago
db.getCollection('session').find({}).forEach(
    function(doc) {

        var value = doc.cookieValue;
        if (value) {
            doc.cookie = {
                name: 'quickrant-uuid',
                value: value
            };
            delete doc.cookieValue;
        }

        db.getCollection('session').save(doc);
   }
);