Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Retrieving content from couchdb using CouchDB::Client

by shivam99aa (Novice)
on Apr 23, 2015 at 13:36 UTC ( [id://1124395]=perlquestion: print w/replies, xml ) Need Help??

shivam99aa has asked for the wisdom of the Perl Monks concerning the following question:

I am able to create new documents using CouchDB::Client as well as able to verify if any doc is present. What i am not able to do is to retrieve the contents of any doc. The reason is i am not able to get the correct syntax which is to be used for it. I am not a perl genius so taking a look at the source code did not help me either.

use warnings; use CouchDB::Client; my $c = CouchDB::Client->new(uri => 'http://127.0.0.1:5984/'); my $db = $c->newDB('test'); my $doc = $db->newDoc('12345', undef, {'foo'=>'bar'})->create; if ($db->docExists('12345')){ print "hello\n"; } #my $doc=CouchDB::Client::Doc->new($db); print $doc->retrieve('12345');

I am able to create document but then i need to comment that line on next run as this will give storage error. But after commenting i have no way to retrieve the doc as i have no object remaining. But this should not be the constraint as there should be a way to retrieve doc using the db object by giving id to it.

Replies are listed 'Best First'.
Re: Retrieving content from couchdb using CouchDB::Client
by ww (Archbishop) on Apr 23, 2015 at 14:26 UTC

    After reading the doc for CouchDB::Client, the only thing I see that may be helpful is under "Internal Methods" (which has a note: "You will use these at your own risk" -- offering a technique to obtain Couch objects.

    But, perhaps documents for the DB engine will provide some clues. Have you read those (with a microscope?)

    Or, perhaps these results from a Google search (terms: "CouchDB rename document") will be helpful:

    Storing Documents - CouchDB: The Definitive Guide
    guide.couchdb.org/draft/documents.html
    Documents are CouchDB's central data structure. To best understand and use CouchDB, you need to think in documents. This chapter walks you though the ...
    
    CouchDB Document API - Apache Wiki
    https://wiki.apache.org/couchdb/HTTP_Doc...
    Apache Software Foundation
    Jun 5, 2013 - A CouchDB document is simply a JSON object. You can use any JSON structure with nesting. You can fetch the document's revision ...
    
    Document_Update_Handlers - Couchdb Wiki - Apache Wiki
    https://wiki.apache.org/couchdb/Document...
    Apache Software Foundation
    Apr 23, 2014 - The official documentation has moved to http://docs.couchdb.org ... When the request to an update handler includes a document ID in the URL, ...
    
    [CouchDB-user] rename document attachment - Grokbase
    grokbase.com › Groups › CouchDB › user › September 2009
    Sep 3, 2009 - (4 replies) I have a simple html/javascript app that uploads an attachment to a document. However, once it is uploaded I want to rename it to a ...
Re: Retrieving content from couchdb using CouchDB::Client
by GotToBTru (Prior) on Apr 23, 2015 at 14:15 UTC

    Is there a way to determine if newDoc might have failed to create the document? What is the result of running the following?

    use warnings; use CouchDB::Client; my $c = CouchDB::Client->new(uri => 'http://127.0.0.1:5984/'); my $db = $c->newDB('test'); my ($doc) if ($db->docExists('12345')) { print "hello\n"; } else { $doc = $db->newDoc('12345', undef, {'foo'=>'bar'})->create; } print $doc->retrieve('12345');
    Dum Spiro Spero

      CouchDB::Client::Doc=HASH(0x240e8e8)

      This is what i get in return, my point is that i created a document and then i am able to get it since $doc object is present but if the doc is present already in couchdb and i want to retrieve it then what should be the procedure. If i run the same code again then doc is already present in couchdb so i get this as output.

      hello

      Can't call method "retrieve" on an undefined value at couch.pl line 15.

        Perhaps apply the retrieve method to the $db object?

        if ($db->docExists('12345')) { print "hello\n"; $doc = $db->retrieve('12345'); } else { $doc = $db->newDoc('12345', undef, {'foo'=>'bar'})->create; }
        Dum Spiro Spero
Re: Retrieving content from couchdb using CouchDB::Client
by SimonPratt (Friar) on Apr 24, 2015 at 14:12 UTC

    my $doc = $db->newDoc('12345', undef, {'foo'=>'bar'})->create; - this creates a document and stores it into the DB in one line. This can only be done once, without having the DB generate an error and crap out - Expecting otherwise is only something you should do if you're working with MySQL.

    To create a new, blank document object that you can work with, try my $doc = $db->newDoc();, or you could even go so far as my $doc = $db->newDoc->retrieve('12345');

    UPDATE: To my mind this is counter-intuitive, as retrieving a document from the DB should be something you do to the DB, not to a document object

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1124395]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found