Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nanozuki/mongonorm
MongoNorm is Not a Object Relational Mapping library for mongodb.
https://github.com/nanozuki/mongonorm
Last synced: about 2 months ago
JSON representation
MongoNorm is Not a Object Relational Mapping library for mongodb.
- Host: GitHub
- URL: https://github.com/nanozuki/mongonorm
- Owner: nanozuki
- License: bsd-2-clause
- Created: 2017-03-29T16:10:06.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-18T10:02:06.000Z (over 7 years ago)
- Last Synced: 2024-10-12T01:05:24.927Z (3 months ago)
- Language: Python
- Homepage:
- Size: 44.9 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
MongoNorm
=========`中文文档 `_
**MongoNorm** is Not a Object Relational Mapping library for mongodb.
MongoNorm just packages document Mongo's as an object. you can add custom
methods and properties for it, And you can still use it as dict.MongoNorm based on pymongo. The class and methos which is not be mentioned
is same as pymongo's, please refer to pymongo's documentation first.installation
------------
Use pip to install::
pip install MongoNormuse guide
---------1. MongoClient and Database
Use these just like you use in pymongo::
from mongonorm import MongoClient
client = MongoClient()
# Or: client = MongoClient('mongodb://localhost:27017/')
db = client.test_database2. Define your own Model
MongoNorm changed the way collections and documents were used,
first you need to define your own Model::@db.collection('articles')
Class Article(object):
"""Article
documents struct: {
"title": "article title",
"author": "author",
"content": ""
}
"""
def __init__(self, title, author, content):
self.insert({
'title': title,
'author': author,
'content': content})
def html_content(self):
parse_html(self['content'])*!Warning about __init__():*
Don't add attribute in :``__init__``. You can defind property in Model if
you need.You must call ``insert(document)`` in or after ``__init__`` to upload
document to Mongodb.All the pymongo on the collection of operations,
have become the Model of the classmethod,
If the pymongo's return is the document, will be monogonorm package
for your definition of the class, such as::
Article.find_one({'title': 'Hello'}) # return an object of Article or None
cur = Article.find({})
# Return a cursor, you can get Article object from this cusorfor article in cur:
print(article['title']) # use as dict
print(article.html_content()) # use method of model class*!Warning:*
When you modified a document out of your document object, you should
reload it before use it::
Article.update_many({'tags': []})
article.reload()
print(article['tags'])3. some useful methods:
* shortcut for set a single field as a dict::
article['title'] = 'MongoNorm'
# it will auto update to mongodb* shortcut for update self::
article.update({'$set': {'title', 'MongoNorm', 'author': 'Crows'}})
* the same as replace::
article.replace({'$set': {'title', 'MongoNorm', 'author': 'Crows'}})