Likes and comments system via MongoDB


#1

Simply put, what I need to achieve is posts/likes/comments system; all stored in Mongo.

My earlier approach was to create post, comments, likes schema separately (and connect them) - but a more efficient way would be to have likes inside the post object.

eg:

{ 
    "_id": ObjectId("54bb201aa3a0f26f885be2a3"),
    "photo": "grabembythepu55.png",
    "post": "Great, again.",
    "likeCount": 0,
    "likes": []
}

and then on POST /like this needs to happen on the db;

db.photos.update(
    { 
        "_id": ObjectId("54bb201aa3a0f26f885be2a3"), 
        "likes": { "$ne": ObjectId("54bb2244a3a0f26f885be2a4") }
    },
    {
        "$inc": { "likeCount": 1 },
        "$push": { "likes": ObjectId("54bb2244a3a0f26f885be2a4") }
    }
)

so that duplicates would not happen for the same user.

However I am having trouble using $inc and $push via the API docs / using custom API script.

I saw on this response that mongoDB operators are not supported, but in this blog post it uses $set and $inc.

Any pointers how I can implement this best?


#2

Just following up @zynchro. Did you find a solution for this?

Thanks,
@AlexBowen


#3

Hi, thanks for checking up.
No, I didn’t find a good solution for this yet.

I implemented a different method for the time being; which holds likes and comments as separate documents - which is not as optimal as this approach.