I am having issues getting mongodb working with Django, my setup is Mac OS X 10.7. I followed the tutorial available here: http://django-mongodb.org/topics/setup.html . I have tried with both virtualenv and without (this is my first time installing django so I shouldn't have any conflicts).
In settings.py
DATABASES = {
'default' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database'
}
}
In firstapp.models.py
from django.db import models
from djangotoolbox.fields import ListField
class Post(models.Model):
title = models.CharField()
text = models.TextField()
tags = ListField()
comments = ListField()
In my shell (python manage.py shell), I try running:
from mydjango.firstapp.models import Post
post = Post.objects.create();
But I keep getting the following: DatabaseError: could not connect to localhost:27017: [Errno 61] Connection refused (full traceback)
If I switch settings.py to the following:
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'my_database',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '27017',
'SUPPORTS_TRANSACTIONS': False,
},
}
I get this error in the shell: ImproperlyConfigured: port must be an instance of int
Did you setup MongoDB separately? The howto you link doesn't seem to go over the MongoDB configuration. It assumes the database is already running. In any case MongoDB seems down, or is at least listening somewhere else.
The last error ("...instance of int") is just because you specified '27017'
(a string) instead of 27017
in the configuration dictionary. But even then it should be equivalent to the first, simpler configuration.
In case MongoDB is running but you still get this error while trying to connect from another machine, it may be due to Firewall running on the MongoDB server.
I was running into this exact same error on CentOS 6.5 running MongoDB 2.6.0. Disabling firewall on the machine resolved the issue for me.
If you are creating models at models.py then there is an example below
models.py
from mongoengine import *
class UserLocation(Document):
message = StringField(required=True, max_length=200)
You dont need the sqlite3 if you are not using it and only the mongodb then you have an optio to comment it
settings.py
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
Now in
views.py
from .models import UserLocation
def save_function(request):
msg = "shinto"
# saving the data to the database
data = UserLocation(message = msg)
data.save()
# reading the data
read_data = json.loads(UserLocation.objects().to_json())
print read_data
There is also another method and its very simple (you don't need to create models in models.py)
views.py
from pymongo import MongoClient
client = MongoClient(port=27017)
db = client.testing_db # use a database called testing_db
collection = db.files # inside that db a collection called files
def a_new_function():
fooman = {"name" : "shinto", "age" : 25}
collection.insert(fooman)
# if you need to display only the name "shinto
#cursor = collection.find({"name" : "shinto"})
# if you need to display all then empty
cursor = collection.find({})
for document in cursor:
print(document)
could not connect to localhost:27017 Connection refused or you see 111 is because you haven't either installed or opened mongodb in another terminal
For installation on ubuntu do the following
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
if Ubuntu 12.04 (deprecated) then in terminal
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
if Ubuntu 14.04 then in terminal
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
if Ubuntu 16.04 then in terminal
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
After do
sudo apt-get update
sudo apt-get install -y mongodb-org
After installation then start mongodb
sudo service mongod start
To stop mongodb do
sudo service mongod stop
To restart mongodb
sudo service mongod restart
Finally you can use
mongo --host 127.0.0.1:27017
which will solve the issue
Thank You
From your question it seems like you are trying to use Django with Mongodb. In which case why do you need Mongoengine?
The official Mongodb documentation talks about djongo. It works by translating SQL queries into query documents.
You don't need Mongoengine to run it.
All native Django contrib modules (eg. admin, user, session) work without any modification.
MongoEngine requires rewriting Django modules and last I checked, the native admin module didn't run on MongoEngine.
Your existing models run without any ORM translation as well.