如何在不使用模型的情况下在django-nonrel中配置mongoDB索引? [英] How do I configure mongoDB indexes in django-nonrel without using Models?

查看:159
本文介绍了如何在不使用模型的情况下在django-nonrel中配置mongoDB索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongodb和django-nonrel构建一个网站。我已阅读各种地方对于mongo,最好使用直流pymongo比django ORM。这还有我的经验 - django的ORM对于关系数据库来说非常棒,但是对于pymongo来说,并不是没有这么做。



我的问题是我不知道如何设置数据库表(err ...collections),而不使用django的ORM。我需要做什么才能摆脱models.py和syncdb的束缚,并且只需自己编写代码?



似乎有人应该为此创建一个指南已经有,但我找不到一个。



更多的细节:



现在, m构建模型并运行syncdb来配置数据库。到目前为止,django的ORM魔法使其工作。但是,我需要做一些稍微有点奇怪的东西,比如索引子元素,所以我不认为ORM会对我有用。



我不会在项目中的其他任何地方使用模型(除auth_users和session之外)。真正的模式在json的其他地方定义。我不想同时维护模型类和json模式 - 这只是不好的做法。



最后,我有一个loadfixtures管理命令,我用来刷新,syncdb和加载灯具。看起来这将是一个非常好的地方,新的ORM替换代码生活,我只是不知道该代码应该是什么样子....

解决方案

使用MongoDB,您不需要额外的步骤来预先确定模式来设置集合。 MongoDB的文档导向性实际上并不执行严格的模式;集合中的文档可能需要不同的字段。这是一个不同的概念,习惯了,但收集将在您开始保存数据后立即创建。



索引可以使用pymongo的 ensureIndex 收藏。



类似于在数据插入时的集合创建,如果添加索引时也不会创建集合。



应该可以帮助您的文章开始使用:使用MongoDB与Django



如果你刚刚进入MongoDB,你也可能想尝试一下简短的在线教程


I'm building a site using mongodb and django-nonrel. I've read in various places that for mongo, it's better to use straight pymongo than the django ORM. This jives with my experience as well -- django's ORM is awesome for relational databases, but for doesn't give you much that pymongo doesn't do already.

My problem is that I don't know how to set up the database tables (err... "collections") initially without using django's ORM. What do I need to do to cast off the shackles of models.py and syncdb, and just write the code myself?

Seems like somebody should have created a guide for this already, but I can't find one.

A little more detail:

Right now, I'm building models and running syncdb to configure the DB. So far, django's ORM magic has made it work. But I need to do some slightly fancier stuff, like indexing on sub-elements, so I don't think the ORM is going to work for me anymore.

On top of that, I don't use models (other than auth_users and sessions) anywhere else in the project. The real schemas are defined elsewhere in json. I don't want to maintain the model classes when and the json schemas at the same time -- it's just bad practice.

Finally, I have a "loadfixtures" management command that I use to flush, syncdb, and load fixtures. It seems like this would be a very good place for the new ORM-replacing code to live, I just don't know what that code should look like....

解决方案

With MongoDB you don't need an extra step to predeclare the schema to "set up" collections. The document-oriented nature of MongoDB actually does not enforce a strict schema; documents within a collection may have different fields as needed. It's a different concept to get used to, but the collection will be created as soon as you start saving data to it.

Indexes can be added using pymongo's ensureIndex on a collection.

Similar to the collection creation on data insertion, a collection will also be created if it does not exist when an index is added.

An article that should help you get started: Using MongoDB with Django.

If you're new to MongoDB, you also might want to try the short online tutorial.

这篇关于如何在不使用模型的情况下在django-nonrel中配置mongoDB索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆