@@ -775,6 +775,152 @@ def put_attachment(self, doc, content, filename=None, content_type=None):
775
775
}, rev = doc ['_rev' ])
776
776
doc ['_rev' ] = data ['rev' ]
777
777
778
+ def find (self , mango_query , wrapper = None ):
779
+ """Execute a mango find-query against the database.
780
+
781
+ Note: only available for CouchDB version >= 2.0.0
782
+
783
+ More information on the `mango_query` structure can be found here:
784
+ http://docs.couchdb.org/en/master/api/database/find.html#find-selectors
785
+
786
+ >>> server = Server()
787
+ >>> db = server.create('python-tests')
788
+ >>> db['johndoe'] = dict(type='Person', name='John Doe')
789
+ >>> db['maryjane'] = dict(type='Person', name='Mary Jane')
790
+ >>> db['gotham'] = dict(type='City', name='Gotham City')
791
+ >>> mango = {'selector': {'type': 'Person'},
792
+ ... 'fields': ['name'],
793
+ ... 'sort':[{'name': 'asc'}]}
794
+ >>> for row in db.find(mango): # doctest: +SKIP
795
+ ... print(row['name']) # doctest: +SKIP
796
+ John Doe
797
+ Mary Jane
798
+ >>> del server['python-tests']
799
+
800
+ :param mango_query: a dictionary describing criteria used to select
801
+ documents
802
+ :param wrapper: an optional callable that should be used to wrap the
803
+ resulting documents
804
+ :return: the query results as a list of `Document` (or whatever `wrapper` returns)
805
+ """
806
+ status , headers , data = self .resource .post_json ('_find' , mango_query )
807
+ return map (wrapper or Document , data .get ('docs' , []))
808
+
809
+ def explain (self , mango_query ):
810
+ """Explain a mango find-query.
811
+
812
+ Note: only available for CouchDB version >= 2.0.0
813
+
814
+ More information on the `mango_query` structure can be found here:
815
+ http://docs.couchdb.org/en/master/api/database/find.html#db-explain
816
+
817
+ >>> server = Server()
818
+ >>> db = server.create('python-tests')
819
+ >>> db['johndoe'] = dict(type='Person', name='John Doe')
820
+ >>> db['maryjane'] = dict(type='Person', name='Mary Jane')
821
+ >>> db['gotham'] = dict(type='City', name='Gotham City')
822
+ >>> mango = {'selector': {'type': 'Person'}, 'fields': ['name']}
823
+ >>> db.explain(mango) #doctest: +ELLIPSIS +SKIP
824
+ {...}
825
+ >>> del server['python-tests']
826
+
827
+ :param mango_query: a `dict` describing criteria used to select
828
+ documents
829
+ :return: the query results as a list of `Document` (or whatever
830
+ `wrapper` returns)
831
+ :rtype: `dict`
832
+ """
833
+ _ , _ , data = self .resource .post_json ('_explain' , mango_query )
834
+ return data
835
+
836
+ def index (self ):
837
+ """Get all available indexes.
838
+
839
+ Note: Only available for CouchDB version >= 2.0.0 .
840
+
841
+ More information here:
842
+ http://docs.couchdb.org/en/master/api/database/find.html#get--db-_index
843
+
844
+ >>> server = Server()
845
+ >>> db = server.create('python-tests')
846
+ >>> db.index() #doctest: +SKIP
847
+ {'indexes': [{'ddoc': None,
848
+ 'def': {'fields': [{'_id': 'asc'}]},
849
+ 'name': '_all_docs',
850
+ 'type': 'special'}],
851
+ 'total_rows': 1}
852
+ >>> del server['python-tests']
853
+
854
+ :return: `dict` containing the number of indexes (`total_rows`) and
855
+ a description of each index (`indexes`)
856
+ """
857
+ _ , _ , data = self .resource .get_json ('_index' )
858
+ return data
859
+
860
+ def add_index (self , index , ddoc = None , name = None ):
861
+ """Add an index to the database.
862
+
863
+ Note: Only available for CouchDB version >= 2.0.0 .
864
+
865
+ More information here:
866
+ http://docs.couchdb.org/en/master/api/database/find.html#post--db-_index
867
+
868
+ >>> server = Server()
869
+ >>> db = server.create('python-tests')
870
+ >>> db['johndoe'] = dict(type='Person', name='John Doe')
871
+ >>> db['maryjane'] = dict(type='Person', name='Mary Jane')
872
+ >>> db['gotham'] = dict(type='City', name='Gotham City')
873
+ >>> db.add_index({'fields': [{'type': 'asc'}]}, #doctest: +SKIP
874
+ ... ddoc='foo',
875
+ ... name='bar')
876
+ {'id': '_design/foo', 'name': 'bar', 'result': 'created'}
877
+ >>> del server['python-tests']
878
+
879
+ :param index: `dict` describing the index to create
880
+ :param ddoc: (optional) name of the design document in which the index
881
+ will be created
882
+ :param name: (optional) name of the index
883
+ :return: `dict` containing the `id`, the `name` and the `result` of
884
+ creating the index
885
+ """
886
+ assert isinstance (index , dict )
887
+ query = {'index' : index }
888
+ if ddoc :
889
+ query ['ddoc' ] = ddoc
890
+ if name :
891
+ query ['name' ] = name
892
+ _ , _ , data = self .resource .post_json ('_index' , query )
893
+ return data
894
+
895
+ def remove_index (self , ddoc , name ):
896
+ """Remove an index from the database.
897
+
898
+ Note: Only available for CouchDB version >= 2.0.0 .
899
+
900
+ More information here:
901
+ http://docs.couchdb.org/en/master/api/database/find.html#delete--db-_index-designdoc-json-name
902
+
903
+ >>> server = Server()
904
+ >>> db = server.create('python-tests')
905
+ >>> db['johndoe'] = dict(type='Person', name='John Doe')
906
+ >>> db['maryjane'] = dict(type='Person', name='Mary Jane')
907
+ >>> db['gotham'] = dict(type='City', name='Gotham City')
908
+ >>> db.add_index({'fields': [{'type': 'asc'}]}, #doctest: +SKIP
909
+ ... ddoc='foo',
910
+ ... name='bar')
911
+ {'id': '_design/foo', 'name': 'bar', 'result': 'created'}
912
+ >>> db.remove_index('foo', 'bar') #doctest: +SKIP
913
+ {'ok': True}
914
+ >>> del server['python-tests']
915
+
916
+ :param ddoc: name of the design document containing the index
917
+ :param name: name of the index that is to be removed
918
+ :return: `dict` containing the `id`, the `name` and the `result` of
919
+ creating the index
920
+ """
921
+ _ , _ , data = self .resource .delete_json (['_index' , ddoc , 'json' , name ])
922
+ return data
923
+
778
924
def query (self , map_fun , reduce_fun = None , language = 'javascript' ,
779
925
wrapper = None , ** options ):
780
926
"""Execute an ad-hoc query (a "temp view") against the database.
0 commit comments