couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Metson <si...@cloudant.com>
Subject Re: Efficient way to identify documents
Date Thu, 07 Jun 2012 13:38:34 GMT
Hi, 
Moving this to user@

If you just need the authors names, and books are unique, you could just have your book documents
be something like:

{
  _id: book_title,
  author: {
    name: author_name,
    age: author_age
  },
  year: book_year
}

then pull out all the authors via a view. 

You shouldn't include the doc in the view value, that's very space inefficient. I think you'll
want a view like:

// view for books
function(doc) {
  emit(doc._id, 1);
}


//view for authors
function(doc) {
  emit(doc.author.name, 1);
}


(you could use a _count reduce to return the number of books/authors, if thats useful)

The above depends a lot on what you want to use the data for, and what other information you
need to include in your docs, though. You might need to have a list of authors, for example,
or include their biography in which case the above isn't great.
Cheers
Simon

On Thursday, 7 June 2012 at 13:57, pjmorce wrote: 
> Hello,
> 
> I am new to noSQL databases and more precisely to couchDB.
> 
> I have migrated my PostgreSQL database to couchDB.
> 
> Before, in my relational database I had 2 tables: Authors and Books
> 
> Now, for each row of these tables, on my couchDB database I have a document:
> 
> { _id = "author_1"
> 
> {
> name = "a"
> age = "b"
> }
> }
> { _id = "author_2"
> 
> {
> name = "abc"
> age = "bcd"
> }
> }
> { _id = "book_1"
> 
> {
> title = "the x files"
> year = "1994"
> }
> }
> { _id = "book_2"
> 
> {
> title = "the jungle book"
> year = "1964"
> }
> }
> ...
> 
> For getting all the authors I created the following view:
> 
> function(doc) {
> if(doc._id.indexOf('author_') == 0) {
> emit(null, doc);
> }
> }
> 
> and for getting all the books I created the following view:
> 
> function(doc) {
> if(doc._id.indexOf('book_') == 0) {
> emit(null, doc);
> }
> }
> 
> 
> Is there any more efficient way to do this? I think this solution is not
> performant when large amount of documents will be in the database...
> 
> Thank you
> 
> Regards
> 
> --
> View this message in context: http://couchdb-development.1959287.n2.nabble.com/Efficient-way-to-identify-documents-tp7580274.html
> Sent from the CouchDB Development mailing list archive at Nabble.com (http://Nabble.com).
> 
> 



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message