Examples

Basic Usage

from mnj import *
from pymongo import MongoClient


db = MongoClient()['test']
db.docs.drop()
db.docs.insert_one(d([('a', 1), ('b', 1)]))
db.docs.insert_one(d([('a', 2), ('b', 2)]))
db.docs.insert_one(d([('a', 3), ('b', 3)]))
db.docs.insert_one(d([('a', 1), ('b', 4)]))
db.docs.insert_one(d([('a', 2), ('b', 5)]))
db.docs.insert_one(d([('a', 3), ('b', 6)]))

# {'a': 1, 'b': 1}
# {'a': 1, 'b': 4}
for doc in db.docs.find(q(a=1)):
    print(doc)

# {'a': 1, 'b': 1}
# {'a': 3, 'b': 3}
# {'a': 1, 'b': 4}
# {'a': 3, 'b': 6}
for doc in db.docs.find(q(a=ne_(2))):
    print(doc)

# {'a': 1, 'b': 4}
# {'a': 2, 'b': 5}
# {'a': 3, 'b': 6}
for doc in db.docs.find(q(b=gt_(3))):
    print(doc)

# {'a': 2, 'b': 2}
# {'a': 3, 'b': 3}
# {'a': 3, 'b': 6}
for doc in db.docs.find(q(a=3) | q(b=2)):
    print(doc)

# {'a': 1, 'b': 4}
for doc in db.docs.find(and_(q(a=1), q(b=4))):
    print(doc)

How to contribute

Any kind of contributions are always welcome!

We always need help in developing, testing and documenting code. Also we would like to see real world Mnj usage examples and a new cases which aren’t covered by Mnj yet.

All contributions should be performed using Mnj Github Page.

Contributing code

All the code contributions are being accepted via Github Pull Requests.

To be easier accepted pull requests (PR) must meet the following requirements:

  • PR must be targeted to develop branch.
  • PR must be rebased on develop branch before sending.
  • The code must be covered with the unit tests corresponding to the added functionality.
  • The documentation should be updated according to the changes made.

Bugs, use cases, feature requests, questions

Feel free to use Mnj Issue Tracker to provide information regarding bugs found or ask anything.

Simplified BSD License

Copyright (c) 2016, Serge Matveenko 
All rights reserved. 

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are 
met: 

 * Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright 
   notice, this list of conditions and the following disclaimer in the 
   documentation and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY 
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
SUCH DAMAGE.

Mnj Docs ToDo

  • Merge README into docs.
  • Write DSL usage tutorial.
  • Provide DSL operators reference.
  • Write “how to” on DSL extending.
  • Document library using autodoc (see mnj).

Mongo Energy - Simple yet Powerful Python MongoDB Library and ODM

Mnj (Mongo Energy) is a Python library for working with MongoDB in the most pythonic way possible.

Mnj consits of the three main layers:

  • Python based DSL on top of the PyMongo-like syntax.
  • Custom DSL operators implementing additional features which cover MongoDB best practices.
  • ODM (Object Document Mapping) framework that defines document schema and provides a high level abstraction for the Mnj features.

Mnj is licensed under Simplified BSD License.

Mnj DSL is handy

books = col.find(q(
    author=regex_('Gibson', re.IGNORECASE),
    year=gte_(2003)
))

Mnj DSL can be used with any Python MongoDB driver if it is PyMongo compatible, including Motor and txmongo.

Still not convinced?☺ See Examples.

Get Mnj

Install

pip install mnj

Contribute

Any contributions are always welcome! See How to contribute.