Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8C54ADC6D for ; Mon, 5 Nov 2012 22:31:30 +0000 (UTC) Received: (qmail 74031 invoked by uid 500); 5 Nov 2012 22:31:29 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 73991 invoked by uid 500); 5 Nov 2012 22:31:28 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 73983 invoked by uid 99); 5 Nov 2012 22:31:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Nov 2012 22:31:28 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of jens@couchbase.com designates 206.225.164.32 as permitted sender) Received: from [206.225.164.32] (HELO EXHUB020-5.exch020.serverdata.net) (206.225.164.32) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Nov 2012 22:31:22 +0000 Received: from EXVMBX020-1.exch020.serverdata.net ([169.254.4.103]) by EXHUB020-5.exch020.serverdata.net ([206.225.164.32]) with mapi; Mon, 5 Nov 2012 14:31:00 -0800 From: Jens Alfke To: "user@couchdb.apache.org" Date: Mon, 5 Nov 2012 14:31:00 -0800 Subject: Re: Exist test? Thread-Topic: Exist test? Thread-Index: Ac27pTszROxi+t5QStWCEO58tub3Zg== Message-ID: <28887DC7-1DBE-42F9-82B3-18F28B393CCF@couchbase.com> References: <99EA19FD-5ED6-4EDA-B538-2D78DCE47DFF@charter.net> <9D607BF7-197D-47C0-BCF8-3D225405C6CE@couchbase.com> <95FDB8AF-32C2-4056-9D3B-9FC05186F16C@charter.net> <3F2385CF-51CD-4A6F-BB41-DE730655BBCF@charter.net> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org 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 orde= r 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 uni= que within the database, it=92s a good idea to use that attribute (or a tra= nsformed/canonicalized version of it) as the document ID. That way the data= base itself can enforce the uniqueness. Otherwise you can end up in situati= ons where there are multiple documents with that same attribute value, if y= our 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 brough= t 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 ou= t if the document had already been inserted in the db. That=92s very easy to do. In generic CouchDB terms (I don=92t know DreamSea= t or .NET), you would * 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=92ll get back a list of the document(s), if an= y, with that title. * If you get back zero rows from the query, create a new document. =97Jens=