Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB ORM for Python? [closed]

Tags:

python

mongodb

I am trying to migrate from sqlalchemy(SQlite) to using mongodb. I would like schema vertification. I amm looking at mongokit, but I want something which is similar to mappers, so that it would save from the object's property, and not a dict.

i would like a mapper so that i can use existing objects without modifying them.

like image 315
Timmy Avatar asked May 06 '10 14:05

Timmy


People also ask

Do I need ORM for MongoDB?

Using MongoDB removes the complex object-relational mapping (ORM) layer that translates objects in code to relational tables. MongoDB's flexible data model also means that your database schema can evolve with business requirements.

Is PyMongo ORM?

PyMongo. The PyMongo MongoDB ORM for Python is a native and highly popular Python driver for MongoDB. It supports MongoDB versions MongoDB 2.6, 3.0, 3.2, 3.4, 3.6, 4.0, 4.2, 4.4, and 5.0. The package is essentially built to provide Python developers with all the right tools to interact with a MongoDB database.

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

Does ORM work with MongoDB?

MongoDB's document-oriented architecture lends itself very well to ORM as the documents that it stores are essentially objects themselves.


2 Answers

Another option is MongoEngine. The ORM for MongoEngine is very similar to the ORM used by Django.

Example (from the tutorial):

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() 
like image 168
David Narayan Avatar answered Sep 23 '22 01:09

David Narayan


Not being satisfied with either MongoKit or MongoEngine, I decided to write my own object-oriented interface for Python.

I delegated all queries directly to pymongo, so the query syntax there is the same. Mostly, it's just an object-wrapper around the results, with some other helpers like database connection pooling, DBRef support, and other convenience methods to make your life easier.

It's called Minimongo and it's available from github. Happy hacking!

Example:

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 
like image 27
slacy Avatar answered Sep 26 '22 01:09

slacy