Video: Querying attachmentsOnce attachments have been uploaded to your CouchDB database, you'll want to be able to reference them easily. Let's create a view to help us get all of the attachment names. First, let's navigate to the restaurant database. Then head over to the View dropdown, and select Temporary View. If you have the default function that just emits null on the document, click Run. Now, let's narrow that down to the Attachments field, add in a dot, and then underscore, and then attachments.
- Next steps
Viewers: in countries Watching now:
- What is NoSQL?
- Installing CouchDB on Mac, Windows, and Linux
- Storing data
- Nesting document data
- Specifying search criteria
- Reducing data
- Attaching and retrieving images
- Deploying applications
- Securing CouchDB
- Understanding the CAP theorem
Once attachments have been uploaded to your CouchDB database, you'll want to be able to reference them easily. Let's create a view to help us get all of the attachment names. First, let's navigate to the restaurant database. Then head over to the View dropdown, and select Temporary View. If you have the default function that just emits null on the document, click Run. Now, let's narrow that down to the Attachments field, add in a dot, and then underscore, and then attachments.
You'll notice that some of our documents don't have an attachments field, while the other ones do. We want to have a function that only returns the documents that have attachments. So let's go to the exercise files and get one. Open up map.js and copy the function. Then paste that function over the existing map function. The ultimate goal here is to get one file name per document.
This view has everything we need. We have the document name as the key, and the file name as the value. We also have the document ID handy. This will help us construct the URLs we need to point to the images. Now, let's click Save As so we can save this view. For the design document, type menu, and for the view name type items. So now we have this as a permanent view in our system. We can also view the raw data. Remove everything from the question mark down to the root.
So now we have that same data available in JSON format. Creating a view is helpful for getting a clean list of attachment file names. With this view in place, you can now easily build applications based around it.
There are currently no FAQs about Up and Running with NoSQL Databases.