1. Install MongoDB
Mongodb home http://www.mongodb.org/
1, download the required version, address: http://www.mongodb.org/display/DOCS/Downloads
(Take windows as an example, linux should be relatively simple), can refer to the official document: http://www.mongodb.org/display/DOCS/Home
2. Add MongoDB to the Environment Variable
3. Set the path for storing database files, such as d: \ db.
4. Open CMD. Do not close this
Mongod-- Dbpath=D: \ Db \ data
5. Start mongo.
- MongoDB shell version: 1.4.0
- Url: test
- Connecting to: test
- Type "exit" to exit
- Type "help" for help
If this occurs, it indicates the operation is successful.
Do not close this.
STEP 2 Django MongoDB
Official documents for reference: http://django-mongodb.org/topics/setup.html
We recommend using virtualenv to build a virtual development environment. (A python environment with independent directories will be created)
Another reason is that Django-nonrel is used.
Pip install virtualenv
Create Environment
Virtualenv myproject
For linux,
Source myproject/bin/activate
For windows, you need to enable Scripts to run and then activate
Install django-nonrel
Pip install hg + https://bitbucket.org/wkornewald/django-nonrel
Install djangotoolbox
Pip install hg + https://bitbucket.org/wkornewald/djangotoolbox
Install mongodb-engine
Pip install git + https://github.com/django-nonrel/mongodb-engine
For windows, you can directly consider (gitmecurial needs to be installed) and open the three webpages, download the files, install
Python steup. py install
After installation, add djangotoolbox to settings. py.
Modify settings. py
- DATABASES= {
- 'Default ':{
- 'Engine': 'django _ mongodb_engine ',
- 'Name': 'My _ database'
- }
- }
More settings: http://django-mongodb.org/reference/settings.html
And
Manage. py syncdb
STEP 3 Test
Documents for reference: http://cn.docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-django-mongodb-engine/
Create a new app and modify models. py.
- From django. db import models
- From django. core. urlresolvers import reverse
- From djangotoolbox. fields import ListField, EmbeddedModelField
- Class Post (models. Model ):
- Created_at=Models. DateTimeField (Auto_now_add=True,Db_index=True)
- Title=Models. CharField (Max_length=255)
- Slug=Models. SlugField ()
- Body=Models. TextField ()
- Comments=ListField(EmbeddedModelField ('comment '),Editable=False)
- Def get_absolute_url (self ):
- Return reverse ('post ',Kwargs= {"Slug": self. slug })
- Def _ unicode _ (self ):
- Return self. title
- Class Meta:
- Ordering= ["-Created_at"]
- Class Comment (models. Model ):
- Created_at=Models. DateTimeField (Auto_now_add=True)
- Body=Models. TextField (Verbose_name="Comment")
- Author=Models. CharField (Verbose_name="Name",Max_length=255)
Add data
Manage. py shell
Create post
- >>>FromTumblelog. modelsImport*
- >>> Post = Post (
- ... Title ="Hello World! ",
- ... Slug ="Hello-world",
- ... Body ="Welcome to my new shiny Tumble log powered by MongoDB and Django-MongoDB! "
- ...)
- >>> Post. save ()
Create comments
- >>>Post. comments
- []
- >>> Comment=Comment(
- ...Author="Joe Bloggs",
- ...Body="Great post! I'm looking forward to reading your blog")
- >>>Post. comments. append (comment)
- >>>Post. save ()
Check Post
- >>> Post = Post. objects. get ()
- >>> Post
- <Post: Hello World!>
- >>> Post. comments
- [<Comment: Comment object>] </
Conclusion: I still don't want to use it, although I always want to know the principle. But there is a gap with the imagination.