Search code examples
mongodbslugdoctrine-odm

How does one generate a slug from user comments dynamically?


I was reading an article on Mongo site where by they mention adding a slug to every user comment. http://docs.mongodb.org/manual/use-cases/storing-comments/

What I am stuck on is how to generate a slug dynamically?

Any tips?


Solution

  • You do this within an area of your comment creation known as before_save. This is basically an event that occurs after you have the information for the comment but you have not saved yet.

    This slug is just a unique identifier, you don't have to use the one they provided and infact the one they provide might not be best for storage, whereby they use the date and time and a bit on the end to make it unique.

    I personally make a slug out of the _ids of the current and previous documents and then separate with /, it works and sorts well also it's easy to use pre-fixed regexes on since it is just the string representation of the OjectId so less guess work needed.