def test_filter_and(self): coll = self.coll df = self.df y = int(df.y.unique()[0]) result = Filter(coll, x=0, y=y).value testdf = df[(df.x == 0) & (df.y == y)] self.assertTrue(result.equals(testdf))
def test_filter_near(self): om = self.om # create a dataframe with geo locations geodf = pd.DataFrame(locations) geodf['location'] = geodf.location.apply(lambda v: GeoJSON(v)) om.datasets.put(geodf, 'geosample', append=False, index='@location') coll = om.datasets.collection('geosample') # closest place result = Filter(coll, location__near=dict(location=(8.541694, 47.3768866), maxd=1)) places = result.value.place.unique() self.assertEqual(places, ['Zurich']) # ordered by distance result = Filter(coll, location__near=dict(location=(8.541694, 47.3768866))) places = list(result.value.place.unique()) self.assertListEqual(places, 'Zurich,Bern,Geneva,New York'.split(',')) # use tuple (lon, lat, maxd) result = Filter(coll, location__near=(8.541694, 47.3768866, 1)) places = list(result.value.place.unique()) self.assertListEqual(places, 'Zurich'.split(',')) # use tuple (lon, lat, mind, maxd) result = Filter(coll, location__near=(8.541694, 47.3768866, 0, 100)) places = list(result.value.place.unique()) self.assertListEqual(places, 'Zurich'.split(','))
def __iter__(self): """ for each group returns the key and a Filter object""" groups = self._count() for group in groups: keys = group.get('_id') data = Filter(self.collection, **keys) yield keys, data
def _get_filter_criteria(self, *args, **kwargs): """ return mongo query from filter specs this uses a Filter to produce the query from the kwargs. :param args: a Q object or logical combination of Q objects (optional) :param kwargs: all AND filter criteria """ if len(args) > 0: q = args[0] if isinstance(q, MongoQ): filter_criteria = Filter(self.collection, q).query elif isinstance(q, Filter): filter_criteria = Filter(self.collection, q.q).query else: filter_criteria = Filter(self.collection, **kwargs).query return filter_criteria
def __init__(self, collection, query=None, projection=None, **kwargs): if isinstance(collection, (Collection, PickableCollection)): database = collection.database name = collection.name else: raise ValueError('collection should be a pymongo.Collection') query = query or {} super(FilteredCollection, self).__init__( database, name, create=False, **kwargs) self.query = Filter(self, **query).query self.projection = projection
def query(self): return Filter(self.collection, **self._fixed_query).query
def test_filter_or(self): coll = self.coll df = self.df result = Filter(coll, x=0, y__gt=5).value testdf = df[(df.x == 0) & (df.y > 5)] self.assertTrue(result.equals(testdf))
def test_filter_in(self): coll = self.coll df = self.df result = Filter(coll, x__in=[1, 2, 3]).value testdf = df[df.x.isin([1, 2, 3])] self.assertTrue(result.equals(testdf))
def test_filter(self): coll = self.coll df = self.df result = Filter(coll, x=0).value testdf = df[df.x == 0] self.assertTrue(result.equals(testdf))
def test_filter_subdoc(self): coll = self.coll coll.update_many(qops.IS(x=qops.LT(5)), qops.SET('subdoc.a', 99)) coll.update_many(qops.IS(x=qops.GTE(5)), qops.SET('subdoc.a', 0)) result = Filter(coll, subdoc__a__lt=10).value self.assertEqual(set(result.x.unique()), set(range(5, 10)))