This document describe the detail of View API.
Sample usage of a view :
>>> from simplecouchdb import Server, Database, Views
>>> server = Server()
>>> db = Database(name="test", server=server)
>>> results = db.view.get("test/all")
at this step, ou have only set an instance of ViewResults.
On only retrieved results when you for example iter results :
>>> list_of_results = list(results)
This class is returned by Views object. ViewResults object can be constructed and passed around without hitting the CouchDB server. No database activity actually occurs until you do something to evaluate the viewresult.
You can evaluate a ViewResult in the following ways:
Iteration: A ViewResult is iterable and it execute the resquest to CouchDB the first time you execute it.
len(). A ViewResults is evaluated when you call len() on it. This, as you might expect, returns the length of the result list.
list(). Force evaluation by calling list() on it
list = list(db.view("test/all"))
Description of the class
Usage example:
>>> db = self.server.create_db('simplecouchdb_test')
>>> doc1 = { '_id': 'test', 'lastname': 'doo', 'firtsname': "john", 'docType': 'identity' }
>>> db.save(doc1)
>>> doc2 = { '_id': 'test2', 'lastname': 'dupont', 'firstname': "jean", 'docType': 'identity'}
>>> db.save(doc2)
>>> design_doc = { '_id': '_design/test', 'language': 'javascript',
'views': { 'all': { "map": """function(doc) {
if (doc.docType == "identity") { emit(doc._id, doc); }}""" } }}
>>> db.save(design_doc)
>>> results = db.view.get('test/all')
>>> len(results)
2
>>> self.server.delete_db('simplecouchdb_test')
This object is used to retrieve result from a couchdb temp view. It is inherited from simplecouchdb.core.View object.
Usage example:
>>> db = self.server.create_db('simplecouchdb_test')
>>> db = self.server.create_db('simplecouchdb_test')
>>> doc1 = { '_id': 'test', 'lastname': 'doo', 'firtsname': "john", 'docType': 'identity' }
>>> db.save(doc1)
>>> doc2 = { '_id': 'test2', 'lastname': 'dupont', 'firstname': "jean", 'docType': 'identity'}
>>> db.save(doc2)
>>> design_doc = { "map": """function(doc) { if (doc.docType == "identity") { emit(doc._id, doc); }}""" }
>>> results = db.slow_view.get(design_doc)
>>> len(results)
2
>>> self.server.delete_db('simplecouchdb_test')
This view object nherited from simplecouchdb.core.View, is only used by simplecouchdb.core.Database.documents()
This object is used o create and register views for a database.
Example usage:
>>> view = ViewDefinition('test', 'all', map_fun= """function(doc)
{ if (doc.docType == "identity") { emit(doc._id, doc);}}""")
>>> db = self.server.create_db('simplecouchdb_test')
>>> doc1 = { '_id': 'test', 'string': 'test', 'number': 4, 'docType': 'test' }
>>> db.save(doc1)
>>> doc2 = { '_id': 'test2', 'string': 'test', 'number': 2, 'docType': 'test'}
>>> db.save(doc2)
>>> view.sync(db)
>>> results = db.view.get('test/all')
>>> len(results)
2
>>> self.server.delete_db('simplecouchdb_test')
See also
Reference Database class