Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Querying a post by date (year and month) and slug

Querying a post by date (year and month) and slug

Problem

I am attempting to create a blog system using Node.js and Express.

Each article has a slug, which is saved into the database. For example, hello-world. Currently I can access a post with just it's slug, such as /article/hello-world.

However, I want the URLs to look more like this: /article/:year/:month/:slug. For example, /article/2011/07/hello-world. How would I do this? I am using MongoDB's findOne method currently, and in the database I have saved a timestamp in a field called created_at.

Problem courtesy of: user1082754

Solution

You can do a date range query:

db. article.findOne({
    created_at: { $gte: ISODate("2012-04-01"), $lte: ISODate("2012-04-30") }, 
    slug: "hello-world"
})

Your specific client language (node.js in this case) I'm sure provides easier ways to work with the date objects.

Solution courtesy of: jdi

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Querying a post by date (year and month) and slug

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×