Programing

Python 용 MongoDB ORM?

crosscheck 2020. 9. 25. 07:28
반응형

Python 용 MongoDB ORM? [닫은]


sqlalchemy (SQlite)에서 mongodb 사용으로 마이그레이션하려고합니다. 스키마 확인을 부탁합니다. mongokit을보고 있지만 매퍼와 유사한 것을 원하므로 dict가 아닌 객체의 속성에서 저장됩니다.

기존 개체를 수정하지 않고 사용할 수 있도록 매퍼를 원합니다.


또 다른 옵션은 MongoEngine 입니다. MongoEngine의 ORM은 Django에서 사용하는 ORM과 매우 유사합니다.

예제 (자습서에서) :

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)

class TextPost(Post):
    content = StringField()

class ImagePost(Post):
    image_path = StringField()

class LinkPost(Post):
    link_url = StringField()

MongoKit 또는 MongoEngine에 만족하지 않아 저는 Python 용 객체 지향 인터페이스를 직접 작성하기로 결정했습니다.

모든 쿼리를 pymongo에 직접 위임 했으므로 쿼리 구문이 동일합니다. 대부분은 데이터베이스 연결 풀링, DBRef 지원 및 기타 편리한 방법과 같은 다른 도우미와 함께 결과를 둘러싼 객체 래퍼 일뿐입니다.

Minimongo 라고 하며 github에서 사용할 수 있습니다. 해피 해킹!

예:

from minimongo import Model, MongoCollection 

class MyObject(Model): 
    model = MongoCollection(database='test', collection='my_collection')

m = MyObject()
m.x = 1
m.field = 'value'
m.other = {'list': True}
m.save()

x = MyObject({'x': 1, 'y': 2}).save()

objs = MyObject.find({'x': 1})
for o in objs: 
    print o

당신이 원하는 MongoKit을 . PyMongo 보다 높은 추상화 계층입니다 . Django를 사용하고 있는지 확실하지 않지만 django-mongokit 통합도 있습니다.

블로그 게시물의. 구조가 정의되면 Computer의 인스턴스가 make / model을 직접 참조 할 수 있습니다 (예 : atari.make, c64.model, ...). 사전 필요 없음 :

import datetime 
from mongokit import Document

class Computer(Document):

    structure = { 
      'make': unicode, 
      'model': unicode, 
      'purchase_date': datetime.datetime, 
      'cpu_ghz': float, 
    }

    validators = { 
      'cpu_ghz': lambda x: x > 0, 
      'make': lambda x: x.strip(), 
    }

    default_values = { 
      'purchase_date': datetime.datetime.utcnow, 
    }

    use_dot_notation = True

    indexes = [ 
      {'fields': ['make']}, 
    ]

I know I'm really late to this question, but I'm the author of Ming http://merciless.sourceforge.net, a SQLAlchemy-inspired MongoDB validation and ORM engine. It's what we use at SourceForge, and there's a reasonable presentation available at http://www.slideshare.net/rick446/rapid-and-scalable-development-with-mongodb-pymongo-and-ming as well as a case study on migrating from SQLAlchemy to Ming http://www.slideshare.net/__amol__/from-sqlalchemy-to-ming-with-turbogears2. Here's an example of the ORM layer in Ming (from the tutorial):

class WikiPage(MappedClass):

    class __mongometa__:
        session = session
        name = 'wiki_page'

    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(str)
    text = FieldProperty(str)
    comments=RelationProperty('WikiComment')

Queries use the standard MongoDB query syntax (not Django ORM's magic keyword arguments):

WikiComment.query.find(dict(page_id=wp._id))

참고URL : https://stackoverflow.com/questions/2781682/mongodb-orm-for-python

반응형