Delta
Delta

Reputation: 2022

Is a good Idea to create lots of couchdb DB's to store comments?

I need to do a message application and I want to use couchdb.

the message document is like this one:

message = {
   'from':'uuid1',
   'to':'uuid2',
   'message':'asd asd asd',
   'date':1342643405.84
}

I need to filter to get the messages aimed to a user and do a pagination by date.

So to filter I have created a view ... emit([doc.from, doc.to], doc) ...

And to do a better pagination than just keeping track of the keys, I Want to create a new db every day, week, or month to store the messages. The databases name can be something like this (this is by day):

...
message_2012/07/01
message_2012/07/02
message_2012/07/03
...

This way I can hit just a part of the database but I don't know if it is a good or a bad Idea.

Upvotes: 1

Views: 110

Answers (1)

smathy
smathy

Reputation: 27961

The way that CouchDB constructs her indexes (using a B-Tree) means that as long as you create your views appropriately, you don't really need to worry about what you're worrying about.

You will need to create your views using the date as the first key in the composite key you're emitting. And you'll need a separate view per type-of-date that you want to page/group/sort by. So if you want to be able to select documents per day, week, month and year then you'll need four views - one for each different type-of-date.

Just be sure to convert your date to a format that can be sorted/searched/grouped lexically. Eg, your monthly view could be:

function(d) {
  if( d.from && d.to && d.date ) {
    var date = new Date( d.date ),
      month = ('0' + (1 + date.getMonth())).slice(-2),
      year = date.getYear();

    date_string = date.getFullYear() + '-' + month;

    emit( [ date_string, d.from, d.to ] );
  }
}

Upvotes: 1

Related Questions