Skip to content

Commit

Permalink
Version 1.1.5
Browse files Browse the repository at this point in the history
Merge pull request #15 from Djcharles26/work
  • Loading branch information
Djcharles26 authored Aug 12, 2021
2 parents 9de5c87 + e60d5e9 commit af6fe96
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
## Minor updates
- Added id in fromJson function in mongo_types
- Added test_count in tests
- Added count_documents method in methods
- Added count method to Schema class and fixed find method
11 changes: 11 additions & 0 deletions pymongoose/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ def _convert_id_to_object_id(id) -> ObjectId:
id = ObjectId(id)
return id

def count_documents(schema: str, query: dict) -> int:
global schemas
retval = -1
try:
schema_name = schema
retval = database[schema_name].count_documents(query)
except:
raise MongoException(message="Error counting document(s)", mongoError=MongoError.Bad_action, bt=sys.exc_info())

return retval

def find(schema: str, query: dict, select = {}, populate=None, one=False, skip = 0, limit=None, sort=None):
global schemas
schema_name = schema
Expand Down
19 changes: 18 additions & 1 deletion pymongoose/mongo_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,23 @@ def exists(cls, query):
retval = methods.exists(cls.collection, query)
return retval

@classmethod
def count(cls, query: dict) -> int:
"""
Count a number of documents inside a collection by query
# Parameters
------------
### query: dict
Dictionary containing query
# Returns
------------
- int
"""
if methods.debug_log:
Logger.printLog(cls.schema_name + " Count")
retval = methods.count_documents(cls.schema_name, query)
return retval

@classmethod
def find(cls, query, select = None, populate=None, one=False, skip = 0, limit=None, sort=None, parse=True):
"""
Expand Down Expand Up @@ -372,7 +389,7 @@ def find(cls, query, select = None, populate=None, one=False, skip = 0, limit=No
"""
if methods.debug_log:
Logger.printLog(cls.schema_name)
retval = methods.find(cls.schema_name, query, select, populate, one)
retval = methods.find(cls.schema_name, query, select, populate, one, skip, limit, sort)
if one:
retval = cls.parse(retval) if parse else retval
return retval
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pymongoose
version = 1.1.4
version = 1.1.5
author = Juan Carlos Lara
author_email = jlaraanaya@gmail.com
description = A pymongo helper with methods and classes
Expand Down
11 changes: 11 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ def test_find_sort():
raise Exception("Error initializing database")
return 1

def test_count():
mongo_init()
try:
user_count = User.count({})
if user_count != -1:
return 0
else:
raise Exception("No users were count")
except:
raise Exception("Error counting")

def test_find_by_id():
mongo_init()
try:
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.4
1.1.5

0 comments on commit af6fe96

Please sign in to comment.