0

Have a few question about mongodb with django!

1) What will be the best package/adaptor to use mongodb with django?

2) Can I create mongo collections without pre defining it's schema? like this one

from pymongo import MongoClient
client = MongoClient()
clientMongoClient('localhost', 27017)
client.database_names()
db = client.mydb
post = {"author": "Mick",
    "text": "My very first blog post",
    "tags": ["mongodb", "python", "pymongo"],
    "date": datetime.datetime.utcnow()}
posts = db.posts
db.collection_names()
>>>[u'testData', u'system.indexes', u'mydb', u'posts']
# a new collection is created

If yes how?

3) Then how easily can I query it?

subha.py
  • 463
  • 3
  • 18
  • Check this `http://django-mongodb-engine.readthedocs.io/en/latest/topics/setup.html` and `https://django-mongodb-engine.readthedocs.io/en/latest/tutorial.html` – Piyush S. Wanare Nov 24 '16 at 06:07
  • That doesn't help again @PiyushS.Wanare – subha.py Nov 24 '16 at 12:11
  • 1) if you want to use mongodb the same way, as you use collections in Django, try mongoengine, it replicates Django ORM well; 2) you can define collection as a `DynamicDocument`: `class MyCollection(DynamicDocument): pass` and it won't require any specific schema 3) Mongoengine query syntax is similar do Django ORM: `MyCollection.objects.all() or MyCollection.objects.get(id=1)` will retrieve all documents in your collection or only specific one. See: http://docs.mongoengine.org/ – Boris Burkov Nov 27 '16 at 21:47
  • 1
    I am using pymongo for now, I really wanted to use django orm, but I also have to attach collections with user, and other complex stuffs. So for now it will be better to use pymongo I guess. Thanks for the reply MyCollection(DynamicDocument): pass is a great trick. @Bob – subha.py Nov 28 '16 at 13:52

0 Answers0