Database: Saving and querying data

class couchdbreq.database.Database(server, name, create=False, get_or_create=False, is_verify_existance=True)[source]

Provides access to a CouchDB database

Do not construct directly. Use couchdbreq.Server.get_db() or couchdbreq.Server.create_db().

all_docs(schema=None, startkey=<object object>, endkey=<object object>, keys=None, key=<object object>, startkey_docid=<object object>, endkey_docid=<object object>, stale=None, descending=False, skip=0, limit=None, group=<object object>, group_level=<object object>, reduce=<object object>, include_docs=False, inclusive_end=True, update_seq=False)[source]

Get all documents from a database

You can use all(), one(), first() on the returned View object just like a View from couchdbreq.database.view().

Returns:couchreq.view.View
changes(since=None, limit=None, descending=False, filter=None, include_docs=False, style='main_only')[source]

Get changes from the db

Only feed=normal is supported because other feed types involve integration with a mainloop

compact()[source]

Compact database

compact_view(view_name)[source]

Compact a view

Parameters:view_name – The name of the view _design/<view_name>
copy_doc(doc, dest=None)[source]

Copy an existing document to a new id. If dest is None, a new uuid will be requested

Parameters:
  • doc – dict or string, document or document id
  • dest – basestring or dict. if _rev is specified in dict it will override the doc
delete_attachment(doc, name)[source]

Delete attachment on the document

Parameters:
  • doc – dict
  • name – name of attachment (unicode or str)
Returns:

dict, with member ok set to True if delete was ok.

delete_doc(doc)[source]

Delete a document

The document will have a _deleted field set to true.

Parameters:doc – The doc
Returns:dict like: {“ok”:true,”rev”:”2839830636”}
delete_docs(docs, all_or_nothing=False)[source]

Delete many docs at once.

It adds ‘_deleted’ member to doc then uses bulk_save to save them.

Parameters:all_or_nothing – In the case of a power failure, when the database

restarts either all the changes will have been saved or none of them. However, it does not do conflict checking, so the documents will

See also

HTTP Bulk Document API <http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API>

ensure_full_commit()[source]

Commit all docs in memory

This can be used after saving docs with batch=True to ensure they are all saved to disk.

fetch_attachment(id_or_doc, name, stream=False, stream_chunk_size=16384)[source]

Get an attachment in a document

Note: If you have stored text e.g. utf8 in the attachment you will need to decode the response to this call using .decode(‘utf8’).

Parameters:
  • id_or_doc – str or dict, doc id or document dict
  • name – name of attachment (unicode or str)
  • stream – boolean, if True return a file object
  • stream_chunk_size – Size in bytes to return per stream chunk (default 16 * 1024)
Returns:

Bytestring or file like iterable if stream=True

get_doc(docid, rev=None, schema=None)[source]

Get document from database

Parameters:
  • docid – str, document id to retrieve
  • rev – Get a specific revision of a document
  • schema – A schema to pass. This is an object with a function wrap_doc(doc)

which will be used to map the response.

Returns:dict, representation of CouchDB document as a dict.
Raise:couchdbreq.exceptions.InvalidDocNameError if the docid is invalid
get_fragmentation()[source]

Get the fragmentation on this database 0 <= fragmentation <= 100 See http://wiki.apache.org/couchdb/Compaction

get_info()[source]

Get database information

Returns:dict
get_rev(docid)[source]

Get last revision from docid (the ‘_rev’ member)

Parameters:docid – str, undecoded document id.
Return rev:str, the last revision of document.
get_server()[source]

Get the couchdbreq.server.Server object hosting this database :return: couchdbreq.server.Server

get_view_group_info(view_group)[source]

Get the info of a view group

Parameters:view_group – ‘designname’
Returns:info
length()[source]

Count the number of documents in the database.

This is implemented with a HEAD request to the database.

put_attachment(doc, content, name=None, content_type=None, content_length=None)[source]

Add attachment to a document

If you are storing unicode text then you must encode before passing it to this function. e.g. db.put_attachment(doc, u”Some unicode £”.encode(“utf8”), “My unicode attachment”, “text/plain”)

Parameters:
  • doc – dict
  • content – str or file like object.
  • name – name of attachment (unicode or str) encoded as utf8
  • content_type – string, mimetype of attachment. If you don’t set it, it will be autodetected.
  • content_lenght – int, size of attachment in bytes
Returns:

bool, True if everything was ok.

save_doc(doc=None, encode_attachments=True, batch=False)[source]

Save a document. It will use the _id member of the document or request a new uuid from CouchDB. IDs are attached to documents on the client side because POST has the curious property of being automatically retried by proxies in the event of network segmentation and lost responses.

Parameters:
  • doc – dict. doc is updated with doc ‘_id’ and ‘_rev’ properties returned by CouchDB server when you save.
  • batch – If true then use reduced guarantee that the document has been saved. The _rev field will not be updated.
Returns:

doc updated with ‘_id’ and ‘_rev’

Raise:

couchdbreq.exceptions.ResourceConflict if the save generated a conflict

save_docs(docs, use_uuids=True, all_or_nothing=False)[source]

Save multiple docs at once

Parameters:
  • docs – list of docs
  • use_uuids – add _id in doc who don’t have it already set.
  • all_or_nothing – In the case of a power failure, when the database

restarts either all the changes will have been saved or none of them. However, it does not do conflict checking.

See also

HTTP Bulk Document API <http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API>

view(view_name, schema=None, startkey=<object object>, endkey=<object object>, keys=None, key=<object object>, startkey_docid=<object object>, endkey_docid=<object object>, stale=None, descending=False, skip=0, limit=None, group=<object object>, group_level=<object object>, reduce=<object object>, include_docs=False, inclusive_end=True, update_seq=False)[source]

Query for view results

Parameters:
  • view_name – ‘designname/viewname’
  • schema – A schema to pass. This is an object with a function wrap_row(row)

which will be used to map the response.

Returns:couchreq.view.View
view_cleanup()[source]

Clean up views