couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jens Alfke <>
Subject Re: Exist test?
Date Mon, 05 Nov 2012 22:31:00 GMT

On Nov 5, 2012, at 12:17 PM, Kevin Burton <> wrote:

> When I create a new document both the id and rev will be null. So in order to determine
if the create is a duplicate another field must be involved.

Gotcha. In general, if you have an attribute of a document that must be unique within the
database, it’s a good idea to use that attribute (or a transformed/canonicalized version
of it) as the document ID. That way the database itself can enforce the uniqueness. Otherwise
you can end up in situations where there are multiple documents with that same attribute value,
if your app supports multiple clients or does replication.

> For example say my document had a field 'title' I could construct a view to return all
documents that have a given 'title'. The reason that I brought this up in this context is
that if I could check to see if there were any documents that had a particular title then
I would be closer to finding out if the document had already been inserted in the db.

That’s very easy to do. In generic CouchDB terms (I don’t know DreamSeat or .NET), you
* Create a design document containing a view with a map function:
	function(doc) {if (doc.title) emit(doc.title, null);}
* Query the view for a particular title by setting the startkey and endkey parameters to the
title. You’ll get back a list of the document(s), if any, with that title.
* If you get back zero rows from the query, create a new document.

View raw message