一个受django orm启发的mongo odm。

dongo的Python项目详细描述


一个受django orm启发的mongo odm。

与Python2.7和3.x兼容,并要求数据库服务器至少运行MongoDB 2.6,正如PyMongo所做的那样。

安装

来自PYPI:

$ pip install dongo

从项目根目录:

$ python setup.py install

用法

dongo是一个基于django orm的面向mongodb的odm。

下面是一些查询和类语法的示例。

设置

您需要首先连接到数据库和主机。默认情况下,本地主机 将选择端口27017,但仍需要指定默认端口 数据库:

from dongo import connect

# For the mydatabase named database on localhost
connect('mydatabase')

# For your mongodb in the private network
connect('mydatabase', host='192.168.1.200')

# For multiple hosts in a replica set
connect('mydatabase', hosts=['10.0.0.100', '10.0.0.101', '10.0.0.102:27018'], replica_set='myrepset0')

# A uri can explicitly be specified as well
connect('mydatabase', uri='mongodb://localhost:27017/')

您可以将集合分成不同的数据库,但这些连接选择 如果未指定数据库,则集合将使用的默认数据库。

接下来,您需要声明一些集合类:

from dongo import DongoCollection
from datetime import datetime

class MusicArtist(DongoCollection):
    # if a specific database other than the default is desired, uncomment this:
    # database = 'myotherdatabase'
    collection = 'music_artists'

# That is all you need to query and read records from the collection "music_artists",
# and the following would create new records and insert them and query for them.

queen = MusicArtist({
    'name': 'queen',
    'lead': 'freddie',
    'songs': ['we are the champions', 'we will rock you'],
    'fans': ['jack', 'jill'],
    'nested': {
        'field1': 1,
        'field2': 2,
    },
})
# insert must be called manually
queen.insert()

# you can use keywords and auto-insert with the "new" classmethod.
queen_stoneage = MusicArtist.new(
    name='queens of the stone age',
    lead='josh',
    songs=['go with the flow', 'little sister'],
    start=datetime(year=1996, month=1, day=1),
    fans=['jack'],
    nested={
        'field1': 1,
        'field2': 222,
    },
)

# queries are simple
for ma in MusicArtist.filter(fans='jack'):
    print('jack likes ' + ma['name'])

# you can even do regex queries and bulk updates
MusicArtist.filter(name__regex='^queen').update(new_field='this is a new field')

# There are many operators, like __gt, __gte, __lt, __lte, __in, __nin, all corresponding to mongo's
# operators like $gt.

# you can do set logic as well with operators: |, &, ~
# for example less than comparisons and checking field existence:
for ma in (MusicArtist.filter(start__lt=datetime(2000, 1, 1)) | MusicArtist.filter(start__exists=0)):
    print('either this music artist started before the year 2000 or their startdate is unknown: ' + ma['name'])

# And you can query inside nested dictionaries

for ma in MusicArtist.filter(nested__field1=1):
    print(ma)

# updating the database or fetching fields is as easy as dictionary access
ma = MusicArtist.filter(name='queen').first()
ma['new_field'] = 'new_value'
print(ma['name'])
ma.set(new_field_2='a', new_field_3='b', new_field_4={'foo': 'bar'})
ma['nested.field1'] = 'new value in nested field'
ma.set(nested__field1='reset that nested field to this value')

您可能需要与记录关联的方法,为此,您只需扩展 类定义:

class Person(DongoCollection):
    collection = 'persons'

    def print_name(self):
        print(self.get('name', 'unknown'))

    def serialize(self):
        return {
            'name': self.get('name'),
            'age': self.get('age', 0),
            'birthday': self.get('start', datetime.min).isoformat(),
            'favorite_color': self.get('color'),
        }

    def change_color(self, new_color):
        # updates record in database as well
        self['color'] = new_color

    @classmethod
    def start_new_year(cls):
        # add 1 to all age values for every record with a field "age"
        cls.filter(age__exists=1).inc(age=1)
        # kill off those 110 and older
        cls.filter(age__gte=110).delete()

    @classmethod
    def startswith(cls, prefix):
        # find all persons with a name that starts with ``prefix``
        regex = '^{}'.format(prefix)
        return cls.filter(name__regex=regex)

    @classmethod
    def endswith(cls, suffix):
        # find all persons with a name that ends with ``suffix``
        regex = '{}$'.format(suffix)
        return cls.filter(name__regex=regex)

    @classmethod
    def first_10(cls):
        return cls.filter().iter(limit=10, sort='name')

    @classmethod
    def sort_by_oldest_first_then_alphabetically(cls):
        return cls.filter().iter(sort=[('age', -1), ('name', 1)])

发行说明

0.4.0:Added DongoBulk functionality, with lazy and bulk operations.
0.3.0:Added Dongo references feature, with instance.ref() and deref
0.2.3:Removed unnecessary dependency
0.2.2:Released alpha with python 2.7 and 3.x compatibility
0.2.1:Released alpha with python 3.x compatibility

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在OSGI中使用cxf生成的客户端时出现Java类装入器问题和JaxB异常   java为什么要在javamail中迭代多部分电子邮件中的部分?   并发编程问题   JFileChooser&&System中未调用java windowClosing。退出功能不正常?   SQL查询的java语法分析   java如何使用AspectJ声明字段上的警告   什么是java向量。元素()C#等价物   java解析Android应用程序中tornado web服务中的CSV文件   java我试过c2dm,我需要服务器端   java调整JPanel大小以适应新的JLabel图标   Java与Python脚本的通信   java使用Saxon通过XSLT生成URL   java net::ERR_complete_CHUNKED_编码200(OK)来自struts应用程序中的tomcat   java如何为我的窗格设置不同的位置?   java使用Cypher Neo4j获取给定类型的所有节点(从SQL世界中的tablename中选择*)   nio使用Java解析文件值   java使用WSDL生成REST客户端会是错误的方向吗?   java如何在我的应用程序中构建类映射?   java按钮。setEnabled在第一个循环中不起作用   xPath适用于最后一页,但不适用于第一页