Skip to content

Commit

Permalink
add an example for how to dump a cursor to json
Browse files Browse the repository at this point in the history
  • Loading branch information
cymoo committed Feb 9, 2020
1 parent 15fa2d3 commit 21876db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ Return a dict corresponding to the model instance.

Return a json string. Some specific types (`ObjectId`, `datetime`, etc.) will be handled correctly.

* `__iter__()`

This model instance is iterable.

#### Class Methods

* `set_db(db)`
Expand All @@ -177,6 +173,24 @@ It proxies a subset of methods in `pymongo.collection:Collection`, which will pe

* `find_one`, `find`, `find_one_and_delete`, `find_one_and_replace`, `find_one_and_update` will convert query results to the corresponding model object.

__`find` returns a `Cursor` of model instances instead of dicts. Before dump your documents to json, remember to do a small conversion.__

```python
from monorm import *

class BinData(Model):
name: str
data: bytes

BinData.set_db(MongoClient().get_database('posts'))
BinData(name='foo', data=b'abc').save()
BinData(name='bar', data=b'xyz').save()

json_dumps([bd.to_dict() for bd in BinData.find()])
# or call pymongo's methods directly
json_dumps(BinData.get_collection().find())
```

-----

#### Meta
Expand Down Expand Up @@ -401,7 +415,7 @@ $ pytest
## Dependencies

* Python >= 3.6
* pymongo >= 3.zzzzzz
* pymongo >= 3.7

## License

Expand Down
2 changes: 1 addition & 1 deletion monorm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
from datetime import datetime
from typing import List, Any

__version__ = '0.0.3'
__version__ = '0.0.4'

0 comments on commit 21876db

Please sign in to comment.