mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Cannot connect to database default : False is not a read preference.

Open nekoctl opened this issue 11 years ago • 38 comments

I use connect(host='mongodb://username:password@localhost:port/database') in my django settings.py. Run server then got a report:

mongoengine.connection.ConnectionError: Cannot connect to database default :
False is not a read preference.

Is this a bug or not? Write in python 3.4.3 with django 1.8 and mongoengine 0.9.0

nekoctl avatar Apr 09 '15 11:04 nekoctl

+1

srinivasreddy avatar Nov 02 '15 11:11 srinivasreddy

+1

maria avatar Nov 22 '15 08:11 maria

pymongo 3.1 is not work.

now, i have to use pymongo 2.8

hhstore avatar Nov 26 '15 06:11 hhstore

@hhstore I think it's released now. Which version of mongoengine do you have?

thedrow avatar Dec 08 '15 05:12 thedrow

Still present in MongoDB shell version: 3.2.5.

riccitensor avatar Apr 27 '16 13:04 riccitensor

Got pymongo 3.2.2, mongoengine 0.9, got this bug.

BurkovBA avatar May 18 '16 00:05 BurkovBA

Is this problem solved yet? I want to upgrade my backend to python3.5 and pymongo 3.0.3 is not catching up with you guys for this error. pymongo 2.8 is supported only to python 3.4. Which forces me down to python 3.4.

faical-yannick-congo avatar Jul 26 '16 20:07 faical-yannick-congo

I am using django 1.11 , python 3.5, mongoengine 0.13.0, pymongo-3.4.0 same error

sathiz1993 avatar May 26 '17 09:05 sathiz1993

+1 mac

jigar1859 avatar Oct 07 '17 07:10 jigar1859

OS X High Sierra, mongoengine 0.15.0, pymongo 3.3.2 same error, if it helps get this resolved sooner.

SwapneelM avatar Nov 17 '17 16:11 SwapneelM

Trying to use mongonengine with django, I have to use mongoengine 0.9.0. So now I also have to downgrade pymongo to 2.8. Not good..

elsampsa avatar May 11 '18 06:05 elsampsa

I am facing this issue as well. Any solution? Downgrading to 2.8 did not solve the issue. Instead, it created another issue.

ishantanu avatar Mar 30 '19 11:03 ishantanu

@ishantanu Can you clarify the problem? You only observe this with dango, right? Can you provide a code snippet that shows the issue?

bagerard avatar Mar 30 '19 19:03 bagerard

@bagerard I am observing it with something built with Flask and python. So, after downgrading pymongo to 2.8.1, I am seeing this issue:

Traceback (most recent call last):
  File "toolbox/collect-tweets.py", line 10, in <module>
    from smm import models
  File "/Users/shantanudeshpande/streamcrab/smm/models.py", line 2, in <module>
    import mongoengine
  File "/usr/local/lib/python2.7/site-packages/mongoengine/__init__.py", line 3, in <module>
    from mongoengine import document
  File "/usr/local/lib/python2.7/site-packages/mongoengine/document.py", line 10, in <module>
    from mongoengine.base import (BaseDict, BaseDocument, BaseList,
  File "/usr/local/lib/python2.7/site-packages/mongoengine/base/__init__.py", line 11, in <module>
    from mongoengine.base.metaclasses import *
  File "/usr/local/lib/python2.7/site-packages/mongoengine/base/metaclasses.py", line 9, in <module>
    from mongoengine.queryset import (DO_NOTHING, DoesNotExist,
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/__init__.py", line 3, in <module>
    from mongoengine.queryset.manager import *
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/manager.py", line 2, in <module>
    from mongoengine.queryset.queryset import QuerySet
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 4, in <module>
    from mongoengine.queryset.base import (BaseQuerySet, CASCADE, DENY, DO_NOTHING,
  File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 20, in <module>
    from mongoengine.context_managers import set_write_concern, switch_db
  File "/usr/local/lib/python2.7/site-packages/mongoengine/context_managers.py", line 2, in <module>
    from pymongo.write_concern import WriteConcern
ImportError: No module named write_concern

And, If I use latest version of pymongo, it shows:

Traceback (most recent call last):
  File "toolbox/collect-tweets.py", line 28, in <module>
    models.connect()
  File "/Users/shantanudeshpande/streamcrab/smm/models.py", line 125, in connect
    mongoengine.connect(**conf)
  File "/usr/local/lib/python2.7/site-packages/mongoengine/connection.py", line 272, in connect
    return get_connection(alias)
  File "/usr/local/lib/python2.7/site-packages/mongoengine/connection.py", line 230, in get_connection
    'Cannot connect to database %s :\n%s' % (alias, e))
mongoengine.connection.MongoEngineConnectionError: Cannot connect to database default :
False is not a read preference.

And, If I run with pymongo 2.9, I get this error:

Traceback (most recent call last):
  File "toolbox/collect-tweets.py", line 125, in <module>
    c.run()
  File "toolbox/collect-tweets.py", line 51, in run
    self.get_tweets()
  File "toolbox/collect-tweets.py", line 80, in get_tweets
    self.save(tweet)
  File "toolbox/collect-tweets.py", line 94, in save
    row.save()
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 389, in save
    object_id = self._save_create(doc, force_insert, write_concern)
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 441, in _save_create
    with set_write_concern(collection, write_concern) as wc_collection:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/context_managers.py", line 248, in set_write_concern
    combined_concerns = dict(collection.write_concern.document.items())

However, on macOS 2.9 works. It does not work on Ubuntu 16.04.

Any idea what might be issue? Mongo is running just fine.

Update:

I made it work by using Mongoengine v 0.10.1. I am not sure why it did not worked with latest version.

ishantanu avatar Mar 31 '19 06:03 ishantanu

Just to make sure I get this right, the error you have with latest mongoengine and latest pymongo is this one: mongoengine.connection.MongoEngineConnectionError: Cannot connect to database default : False is not a read preference. ? Could you print the values of **conf (on the mongoengine.connect(**conf) call)? Also specify the mongodb version you are using

bagerard avatar Mar 31 '19 18:03 bagerard

For those asking if this problem still exists Yeah it still exists, but It worked for me using these versions: Django==2.0.3 mongoengine==0.9.0 pymongo==2.8 python==3.6.4

zentx avatar Nov 26 '19 20:11 zentx

False is not a read preference is the error you'll get if you accidentally pass read_preference=False as a kwarg to MongoClient:

>>> MongoClient(read_preference=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/mongo_client.py", line 761, in __init__
    dict(common.validate(keyword_opts.cased_key(k), v) for k, v in keyword_opts.items())
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/mongo_client.py", line 761, in <genexpr>
    dict(common.validate(keyword_opts.cased_key(k), v) for k, v in keyword_opts.items())
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/common.py", line 764, in validate
    value = validator(option, value)
  File "/Users/shane/Library/Python/3.8/lib/python/site-packages/pymongo/common.py", line 362, in validate_read_preference
    raise TypeError("%r is not a read preference." % (value,))
TypeError: False is not a read preference.

ShaneHarvey avatar Sep 08 '22 19:09 ShaneHarvey

Let's close this, it's old and most likely a problem in the argument of connect

bagerard avatar Sep 08 '22 21:09 bagerard