Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Add count method based on find method #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions sleepymongoose/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,48 @@ def _authenticate(self, args, out, name = None, db = None, collection = None):
else:
out('{"ok" : 1}')

def _count(self, args, out, name = None, db = None, collection = None):
"""
query the database.
"""

if type(args).__name__ != 'dict':
out('{"ok" : 0, "errmsg" : "_find must be a GET request"}')
return

conn = self._get_connection(name)
if conn == None:
out('{"ok" : 0, "errmsg" : "couldn\'t get connection to mongo"}')
return

if db == None or collection == None:
out('{"ok" : 0, "errmsg" : "db and collection must be defined"}')
return

criteria = {}
if 'criteria' in args:
criteria = self._get_son(args['criteria'][0], out)
if criteria == None:
return

fields = None
if 'fields' in args:
fields = self._get_son(args['fields'][0], out)
if fields == None:
return

limit = 0
if 'limit' in args:
limit = int(args['limit'][0])

skip = 0
if 'skip' in args:
skip = int(args['skip'][0])

count = conn[db][collection].find(spec=criteria, fields=fields, limit=limit, skip=skip).count()

out(json.dumps({"count": count}, default=json_util.default))

def _find(self, args, out, name = None, db = None, collection = None):
"""
query the database.
Expand Down