Code Monkey home page Code Monkey logo

mongonorm's Introduction

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 MongoNorm

use 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_database
  1. 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 cusor

for 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'])
  1. 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'}})
    

mongonorm's People

Contributors

nanozuki avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

python3pkg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.