incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Shepelev <>
Subject Re: Re: Extract JSON objects with Python?
Date Fri, 26 Jun 2009 11:45:42 GMT
Probably, we should start with Couch basics.

Database is a set of JSON objects called documents.
Each document is uh.. again, a JSON object (set of key-value pairs)
with two additional fields: _id and _rev. _id is unique identifier of
document in database. _rev is a revision (version) identifier used to
distinct changes to same document.

Now, _id is not forced to anything. You can pass _id when you create
object and specify arbitrary value. You can set it as first name in
your persons database. But, probably, you will come up with another
If you don't specify _id, couchdb generates random one as long
hexadecimal string.

You can make direct queries by _id field to couchdb. With
couchdb-python interface it looks like:
mark = db['Mark'] # IF! you are storing persons documents with first
name as identifier, that will return (!) full document (JSON object)
as python dict into name `mark`.

That said, you don't need to do anything special to get full JSON
objects if you query docs by _id.

Also, you can instruct couchdb to create a view (analog of views in
relational databases), you write a map, and, optionally, reduce
function for your view to generate some data, i.e. filter documents
(persons), by age.
I didn't try to create views via couchdb-python, i've done it in Futon
(couchdb builtin admin console). Futon is accessible with browser at (or other address/port, where you run
couchdb). There you can select your database, "temporary view" from
drop-down list in upper-right corner, put

function map(doc) {
  if (doc.age && doc.age > 17) emit(, doc.age);

into "map function" text area and click run. Couchdb will run the view
against your database. And you don't see anything until it processed
all your documents. Very fast for <5K docs.

Then you will see a table of results, something like this:

Bart            27
Mark           19
Ominuk       24

In fact, couchdb returns a list of JSON objects
{key: "Bart", value: 27, _id: "Bart", _rev: "1-2932932"},...etc

So here, executing views, yes you get only part of documents.
But, also, you can instruct couchdb to return full documents even in
views! Append include_docs=true to your GET query args.

And the couch will return list of something like:
{key: "Bart", value: 27, _id: "Bart", _rev: "1-2932932", doc: {name:
"Bart", age: 27, has_sister: true, alive: false}},...etc

In couchdb-python that would be

RG = db.view("_design/DESIGN-DOC/_view/VIEW-NAME", include_docs=True)
# for permanent view VIEW-NAME stored in design document DESIGN-DOC.
Then you will have RG as generator of results.
R = list(RG) # would read all results from generator into list R

More on that, please, read couchdb-python reference.

On Fri, Jun 26, 2009 at 12:22 PM, <> wrote:
> Ok, but I didn't mean to extract it directly from a local file (sorry if
> I didn't explain my problem properly) but more ask the db a question
> that returns an entire JSON object.
> For example, say I have a db called persons, with a key Mark and a key
> Marcus. They both have the fields (_id and _rev) address,
> phonenumber,... and so on.
> I want my server application (a pylons server btw) to extract this
> information about mark and marcus and present them as JSON objects for
> another server to parse in JS. This is why I'm wondering if I can get a
> JSON object from the db directly instead of asking for address, phone,
> and so on, to build a new JSON object and present.
> It would be even better if someone knew how to do this in python, but
> all ways are good ways :P
> On Jun 26, 2009 10:07 "Sergey Shepelev" <> wrote:
>> If you want to open Couch database as local file and read it's content
>> and
>> try to operate JSON objects inside, that is possible.But it would be
>> very
>> bad idea. Just because there is already written code (couchdb) which
>> does
>> that and provides you with HTTP API.
>> On Fri, Jun 26, 2009 at 11:57 AM, <> wrote:
>> > CouchDB stores objects in JSON format right?
>> >
>> > Is there any way to extract these JSON objects from CouchDB
>> > directly?
>> > The server that extracts from the db are supposed to present JSON
>> > objects, so it would be stupid to create new objects if they are
>> > stored
>> > in the correct syntax.
>> >
>> > Is this possible in Python? (if not, in any other language?)

View raw message