You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Hank Knight <hk...@gmail.com> on 2014/01/06 21:42:43 UTC

CouchDB: Most recently added document that is NOT a design document

I want the ID of the most recently added document that is NOT a design
document.

This returns the ID of the most recently CHANGED document including
design documents:

http://example.com/dbname/_changes?descending=true&limit=1

Re: CouchDB: Most recently added document that is NOT a design document

Posted by Jens Alfke <je...@couchbase.com>.
On Jan 6, 2014, at 5:13 PM, Lars <la...@systemli.org> wrote:

>> It’s not really any different. They appear in the _changes feed just as local changes do.
> 
> But you only get the leaf revisions of the documents, not neccessarily
> the first revision, which "added" the document.

But you’re not going to get the first revision of a locally-created document either, not if it’s been modified since then — you’ll just see it with its current revision/generation. (A document only appears once in the _changes feed at any given moment. The _changes feed is not a complete history of every change in the database. It’s more like a list of the current documents sorted by time last modified.)

—Jens

Re: CouchDB: Most recently added document that is NOT a design document

Posted by Lars <la...@systemli.org>.
On 07.01.2014 01:53, Jens Alfke wrote:
> 
> On Jan 6, 2014, at 4:16 PM, Lars <la...@systemli.org> wrote:
> 
>> It might depend on your definition of "added document". If `PUT
>> /db/docid` and `POST /db` are the only ways to "add", then do what Bob
>> said and stop at the first non-design document with a seq number of 1.
> 
> That’s still going to return documents by order modified, not created.
> 
> I think you mean “generation number”, not “seq number”, as only the first document ever added to the db will have a sequence number of 1. And you’ll only see a generation number of 1 if the doc’s never been updated after the initial add, so this isn’t useful in the general case.

Yep sorry, I mean the number part of the revision. It being 1 is the
only way to tell addition and update apart, though, isn't it?

> 
>> It starts getting tricky if you also include in your definition
>> documents that are created by replication, and I'm not sure how to
>> handle that.
> 
> It’s not really any different. They appear in the _changes feed just as local changes do.

But you only get the leaf revisions of the documents, not neccessarily
the first revision, which "added" the document.

> 
> —Jens
> 

Re: CouchDB: Most recently added document that is NOT a design document

Posted by Jens Alfke <je...@couchbase.com>.
On Jan 6, 2014, at 4:16 PM, Lars <la...@systemli.org> wrote:

> It might depend on your definition of "added document". If `PUT
> /db/docid` and `POST /db` are the only ways to "add", then do what Bob
> said and stop at the first non-design document with a seq number of 1.

That’s still going to return documents by order modified, not created.

I think you mean “generation number”, not “seq number”, as only the first document ever added to the db will have a sequence number of 1. And you’ll only see a generation number of 1 if the doc’s never been updated after the initial add, so this isn’t useful in the general case.

> It starts getting tricky if you also include in your definition
> documents that are created by replication, and I'm not sure how to
> handle that.

It’s not really any different. They appear in the _changes feed just as local changes do.

—Jens

Re: CouchDB: Most recently added document that is NOT a design document

Posted by Lars <la...@systemli.org>.
It might depend on your definition of "added document". If `PUT
/db/docid` and `POST /db` are the only ways to "add", then do what Bob
said and stop at the first non-design document with a seq number of 1.
It starts getting tricky if you also include in your definition
documents that are created by replication, and I'm not sure how to
handle that.

On 07.01.2014 01:01, Robert Newson wrote:
> 
> read _changes?descending=true row by row until you reach a non-design document? The doc to ddoc ratio should be strongly in your favor.
> 
> B.
> 
> On 6 Jan 2014, at 22:00, Stanley Iriele <si...@gmail.com> wrote:
> 
>> Could you do what Jens just mentioned and just make a filter?....that way a
>> seq number plus the filter should get you what you want
>> On Jan 6, 2014 1:28 PM, "Jens Alfke" <je...@couchbase.com> wrote:
>>
>>>
>>> On Jan 6, 2014, at 12:42 PM, Hank Knight <hk...@gmail.com> wrote:
>>>
>>>> I want the ID of the most recently added document that is NOT a design
>>>> document.
>>>
>>> There’s nothing built-in for that. CouchDB doesn’t track the order in
>>> which documents are created, only the order in which they’re changed.
>>>
>>> You could put a “date_created” property in a document and populate it with
>>> a timestamp when the doc is first created; then you can make a view that
>>> emits those as keys, and query it in reverse order.
>>>
>>> —Jens
> 

Re: CouchDB: Most recently added document that is NOT a design document

Posted by Robert Newson <rn...@apache.org>.
read _changes?descending=true row by row until you reach a non-design document? The doc to ddoc ratio should be strongly in your favor.

B.

On 6 Jan 2014, at 22:00, Stanley Iriele <si...@gmail.com> wrote:

> Could you do what Jens just mentioned and just make a filter?....that way a
> seq number plus the filter should get you what you want
> On Jan 6, 2014 1:28 PM, "Jens Alfke" <je...@couchbase.com> wrote:
> 
>> 
>> On Jan 6, 2014, at 12:42 PM, Hank Knight <hk...@gmail.com> wrote:
>> 
>>> I want the ID of the most recently added document that is NOT a design
>>> document.
>> 
>> There’s nothing built-in for that. CouchDB doesn’t track the order in
>> which documents are created, only the order in which they’re changed.
>> 
>> You could put a “date_created” property in a document and populate it with
>> a timestamp when the doc is first created; then you can make a view that
>> emits those as keys, and query it in reverse order.
>> 
>> —Jens


Re: CouchDB: Most recently added document that is NOT a design document

Posted by Stanley Iriele <si...@gmail.com>.
Could you do what Jens just mentioned and just make a filter?....that way a
seq number plus the filter should get you what you want
On Jan 6, 2014 1:28 PM, "Jens Alfke" <je...@couchbase.com> wrote:

>
> On Jan 6, 2014, at 12:42 PM, Hank Knight <hk...@gmail.com> wrote:
>
> > I want the ID of the most recently added document that is NOT a design
> > document.
>
> There’s nothing built-in for that. CouchDB doesn’t track the order in
> which documents are created, only the order in which they’re changed.
>
> You could put a “date_created” property in a document and populate it with
> a timestamp when the doc is first created; then you can make a view that
> emits those as keys, and query it in reverse order.
>
> —Jens

Re: CouchDB: Most recently added document that is NOT a design document

Posted by Jens Alfke <je...@couchbase.com>.
On Jan 6, 2014, at 12:42 PM, Hank Knight <hk...@gmail.com> wrote:

> I want the ID of the most recently added document that is NOT a design
> document.

There’s nothing built-in for that. CouchDB doesn’t track the order in which documents are created, only the order in which they’re changed.

You could put a “date_created” property in a document and populate it with a timestamp when the doc is first created; then you can make a view that emits those as keys, and query it in reverse order.

—Jens