Home | Trees | Indices | Help |
|
---|
|
object --+ | Database
Representation of a database on a CouchDB server.
>>> server = Server('http://localhost:5984/') >>> db = server.create('python-tests')
New documents can be added to the database using the save() method:
>>> doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})
This class provides a dictionary-like interface to databases: documents are retrieved by their ID using item access
>>> doc = db[doc_id] >>> doc #doctest: +ELLIPSIS <Document '...'@... {...}>
Documents are represented as instances of the Row class, which is basically just a normal dictionary with the additional attributes id and rev:
>>> doc.id, doc.rev #doctest: +ELLIPSIS ('...', ...) >>> doc['type'] 'Person' >>> doc['name'] 'John Doe'
To update an existing document, you use item access, too:
>>> doc['name'] = 'Mary Jane' >>> db[doc.id] = doc
The save() method creates a document with a random ID generated by CouchDB (which is not recommended). If you want to explicitly specify the ID, you'd use item access just as with updating:
>>> db['JohnDoe'] = {'type': 'person', 'name': 'John Doe'}
>>> 'JohnDoe' in db True >>> len(db) 2
>>> del server['python-tests']
Instance Methods | |||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
Document |
|
||
|
|||
unicode
|
|
||
tuple
|
|
||
bool
|
|
||
str
|
|
||
|
|||
Document |
|
||
|
|||
dict |
|
||
|
|||
|
|||
|
|||
ViewResults |
|
||
list |
|
||
ViewResults |
|
||
|
|||
Inherited from |
Properties | |
basestring |
name The name of the database. |
Inherited from |
Method Details |
|
|
|
|
|
|
Create a new document in the database with a random ID that is generated by the server. Note that it is generally better to avoid the create() method and instead generate document IDs on the client side. This is due to the fact that the underlying HTTP POST method is not idempotent, and an automatic retry due to a problem somewhere on the networking stack may cause multiple documents being created in the database. To avoid such problems you can generate a UUID on the client side. Python (since version 2.5) comes with a uuid module that can be used for this: from uuid import uuid4 doc_id = uuid4().hex db[doc_id] = {'type': 'person', 'name': 'John Doe'}
|
Create a new document or update an existing document. If doc has no _id then the server will allocate a random ID and a new document will be created. Otherwise the doc's _id will be used to identity the document to create or update. Trying to update an existing document with an incorrect _rev will raise a ResourceConflict exception. Note that it is generally better to avoid saving documents with no _id and instead generate document IDs on the client side. This is due to the fact that the underlying HTTP POST method is not idempotent, and an automatic retry due to a problem somewhere on the networking stack may cause multiple documents being created in the database. To avoid such problems you can generate a UUID on the client side. Python (since version 2.5) comes with a uuid module that can be used for this: from uuid import uuid4 doc = {'_id': uuid4().hex, 'type': 'person', 'name': 'John Doe'} db.save(doc)
|
Compact the database or a design document's index. Without an argument, this will try to prune all old revisions from the database. With an argument, it will compact the index cache for all views in the design document specified.
|
Since: 0.6 |
Delete the given document from the database. Use this method in preference over __del__ to ensure you're deleting the revision that you had previously retrieved. In the case the document has been updated since it was retrieved, this method will raise a ResourceConflict exception. >>> server = Server('http://localhost:5984/') >>> db = server.create('python-tests') >>> doc = dict(type='Person', name='John Doe') >>> db['johndoe'] = doc >>> doc2 = db['johndoe'] >>> doc2['age'] = 42 >>> db['johndoe'] = doc2 >>> db.delete(doc) Traceback (most recent call last): ... ResourceConflict: ('conflict', 'Document update conflict.') >>> del server['python-tests']
Since: 0.4.1 |
|
|
Return information about the database as a dictionary. The returned dictionary exactly corresponds to the JSON response to a GET request on the database URI.
Since: 0.4 |
Delete the specified attachment. Note that the provided
Since: 0.4.1 |
Since: 0.4.1 |
Create or replace an attachment. Note that the provided
Since: 0.4.1 |
Execute an ad-hoc query (a "temp view") against the database. >>> server = Server('http://localhost:5984/') >>> db = server.create('python-tests') >>> db['johndoe'] = dict(type='Person', name='John Doe') >>> db['maryjane'] = dict(type='Person', name='Mary Jane') >>> db['gotham'] = dict(type='City', name='Gotham City') >>> map_fun = '''function(doc) { ... if (doc.type == 'Person') ... emit(doc.name, null); ... }''' >>> for row in db.query(map_fun): ... print row.key John Doe Mary Jane >>> for row in db.query(map_fun, descending=True): ... print row.key Mary Jane John Doe >>> for row in db.query(map_fun, key='John Doe'): ... print row.key John Doe >>> del server['python-tests']
|
Perform a bulk update or insertion of the given documents using a single HTTP request. >>> server = Server('http://localhost:5984/') >>> db = server.create('python-tests') >>> for doc in db.update([ ... Document(type='Person', name='John Doe'), ... Document(type='Person', name='Mary Jane'), ... Document(type='City', name='Gotham City') ... ]): ... print repr(doc) #doctest: +ELLIPSIS (True, '...', '...') (True, '...', '...') (True, '...', '...') >>> del server['python-tests'] The return value of this method is a list containing a tuple for every
element in the If an object in the documents list is not a dictionary, this method looks for an items() method that can be used to convert the object to a dictionary. Effectively this means you can also use this method with mapping.Document objects.
Since: version 0.2 |
Execute a predefined view. >>> server = Server('http://localhost:5984/') >>> db = server.create('python-tests') >>> db['gotham'] = dict(type='City', name='Gotham City') >>> for row in db.view('_all_docs'): ... print row.id gotham >>> del server['python-tests']
|
Retrieve a changes feed from the database. Takes since, feed, heartbeat and timeout options. |
Property Details |
nameThe name of the database. Note that this may require a request to the server unless the name has already been cached by the info() method.
|
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Thu Apr 15 16:22:43 2010 | http://epydoc.sourceforge.net |