Skip to content

Commit

Permalink
Add CouchDB day 2 homework
Browse files Browse the repository at this point in the history
  • Loading branch information
peferron committed Dec 2, 2016
1 parent 346e753 commit 702adf5
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions couch/homework.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,67 @@ $ curl "http:https://localhost:5984/my_new_db/12edea69b62a7136c1efc01e44030ca8/hello.t

Hello, World!
```

## Day 2

### Find, 1.

Keys can be numbers, arrays, or objects.

### Find, 2.

[Querying Options](https://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options)

### Do, 1.

```js
function(artist) {
emit(artist.random, artist.name);
}
```

### Do, 2.

```bash
curl -i "http:https://localhost:5984/music/_design/random/_view/artist?limit=1&startkey=$(ruby -e 'puts rand')"
```

Note that this can return an empty array if the randomly generated `startkey` parameter is greater than all `random` values in the DB.

### Do, 3.

Album view:

```js
function(artist) {
artist.albums.forEach(function(album) {
emit(album.random, artist.name + " - " + album.name);
});
}
```

Track view:

```js
function(artist) {
artist.albums.forEach(function(album) {
album.tracks.forEach(function(track) {
emit(track.random, artist.name + " - " + album.name + " - " + track.name);
});
});
}
```

Tag view (the same tag can appear multiple times):

```js
function(artist) {
artist.albums.forEach(function(album) {
album.tracks.forEach(function(track) {
track.tags.forEach(function(tag) {
emit(tag.random, tag.idstr);
});
});
});
}
```

0 comments on commit 702adf5

Please sign in to comment.