mongokit
MongoKit framework try to keep its simplicity when you manage mongodb in python. MongoKit was developed to be fast and light with KISS and DRY in mind. MongoKit brings structured schema and validation layer on top of the great pymongo driver. Discuss with us on Google group : <a href="http://groups.google.com/group/mongokit">http://groups.google.com/group/mongokit</a> or follow the news on Twitter: <a href="http://twitter.com/namlook">h…</a>
I'm trying to save an object to mongodb with Mongokit. For some reason I am getting a "Connection Error: no collection found", only for this particular class model, Game.
I have defined the __collection__ = 'games'
and __database__
parameters in the model, and registered the model with the connection, db.
When I call
g = db.Game()
g.save()
I get an error "no collection found". It works fine with other class models that I've defined, like User. I can't see anything that I'm doing wrong! Any suggestions?
Source: (StackOverflow)
I am attempting to connect and authenticate to a remote database host (dotcloud, mongolabs, etc) using MongoKit within Flask. Connecting to the server seems to work fine. However I am unable to authenticate to the database. Presumably this should work:
from mongokit import Connection
connection = Connection(my_remote_host, my_remote_port)
connection.my_database.authenticate(my_admin_user, my_admin_password)
the call to authenticate() returns True, yet subsequent calls to fetch data throw:
OperationFailure: database error: unauthorized db
Anyone know what might be happening here?
Source: (StackOverflow)
Working on a Django project that requires a NoSQL store and I believe I've settled on Mongo. I've seen a lot of topics that talk about Mongo and Django, but none that mentioned Django-nonrel, and I can't see why it might have been disqualified, but I have no experience with any of them.
Ideally, I'd like to preserve the SQL store for the simple things, user auth, groups, etc., and use Mongo for the bigger data.
I'd also like to have my Mongo-stored objects as classes, in a Django-ORM style, so that I can have a similar 'feel', but that isn't critical.
Lastly, do any of the above allow me to use Django's multi-database support for anything, or are all my mongo queries effectively 'out of band' from the Django ORM?
If there are any other factors I'm missing that might be important, feel free to clue me in. I know little in this arena.
Source: (StackOverflow)
I am developing a Web application using Flask and MongoDB. And I use (Flask-)MongoKit to define a schema to validate my data.
In my database, there is a collection called "users" (see below) that contains a field "email". I try to create a unique index on that field as specified in the MongoKit documentation (http://namlook.github.com/mongokit/indexes.html). However, when I check the collection indexes via MongoDB client shell, there is no index "email" at all.
I found a similar issue on the net: "unique index does not work" (https://github.com/namlook/mongokit/issues/98)
Does someone has any idea why it does not work?
User collection:
@db.register
class User(Model):
__collection__ = 'users'
structure = {
'first_name': basestring,
'last_name': basestring,
'email': basestring,
'password': unicode,
'registration_date': datetime,
}
required_fields = ['first_name', 'last_name', 'email', 'password', 'registration_date']
default_values = {
'registration_date': datetime.utcnow,
}
# Create a unique index on the "email" field
indexes = [
{
'fields': 'email', # note: this may be an array
'unique': True, # only unique values are allowed
'ttl': 0, # create index immediately
},
]
db.users.getIndexes() output:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "youthmind.users",
"name" : "_id_"
},
]
Note that I also try without 'ttl':0, and I was able to create an index using the following piece of code:
db.users.create_index('email', unique=True)
I think this uses the pymongo Connection object directly.
Thanks in advance for your help.
Source: (StackOverflow)
I'm using MongoKit to perform iteration over a huge amount of data.
During this process my cursor becomes invalid, and I'm getting
OperationFailure: cursor id '369397057360964334' not valid at server
I've read in mailing lists that I can pass parameter timeout=False
to .find()
method, but PyMongo FAQ says that I vave to take care of closing cursor myself.
But I didn't find methods in MongoKit for that.
Do I need to close cursor by hand, and if yes - how can I do it?
Source: (StackOverflow)
I get some very strange error using MongoKit:
>>> from mongokit import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mongokit/__init__.py", line 35, in <module>
from document import Document, ObjectId
File "/usr/local/lib/python2.7/dist-packages/mongokit/document.py", line 48, in <module>
from pymongo.objectid import ObjectId
ImportError: No module named objectid
I tried everything to find the reason but nothing helps.
Actually I googled "ImportError: No module named objectid" error and it has no results at all. I tried to use MongoKit following the simple tutorial http://namlook.github.com/mongokit/introduction.html#a-quick-example and the first line of the tutorial produces this strange error. Below is the log of MongoKit installation.
What I'm doing wrong?
user@server:~$ sudo pip install mongokit
Downloading/unpacking mongokit
Downloading mongokit-0.7.2.tar.gz (69Kb): 69Kb downloaded
Running setup.py egg_info for package mongokit
Downloading/unpacking pymongo>=2.0.1 (from mongokit)
Downloading pymongo-2.2.tar.gz (228Kb): 228Kb downloaded
Running setup.py egg_info for package pymongo
Downloading/unpacking anyjson>=0.2.2 (from mongokit)
Downloading anyjson-0.3.1.tar.gz
Running setup.py egg_info for package anyjson
Installing collected packages: mongokit, pymongo, anyjson
Running setup.py install for mongokit
Running setup.py install for pymongo
building 'bson._cbson' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibson -I/usr/include/python2.7 -c bson/_cbsonmodule.c -o build/temp.linux-i686-2.7/bson/_cbsonmodule.o
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibson -I/usr/include/python2.7 -c bson/time64.c -o build/temp.linux-i686-2.7/bson/time64.o
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibson -I/usr/include/python2.7 -c bson/buffer.c -o build/temp.linux-i686-2.7/bson/buffer.o
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibson -I/usr/include/python2.7 -c bson/encoding_helpers.c -o build/temp.linux-i686-2.7/bson/encoding_helpers.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-i686-2.7/bson/_cbsonmodule.o build/temp.linux-i686-2.7/bson/time64.o build/temp.linux-i686-2.7/bson/buffer.o build/temp.linux-i686-2.7/bson/encoding_helpers.o -o build/lib.linux-i686-2.7/bson/_cbson.so
building 'pymongo._cmessage' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibson -I/usr/include/python2.7 -c pymongo/_cmessagemodule.c -o build/temp.linux-i686-2.7/pymongo/_cmessagemodule.o
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Ibson -I/usr/include/python2.7 -c bson/buffer.c -o build/temp.linux-i686-2.7/bson/buffer.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-i686-2.7/pymongo/_cmessagemodule.o build/temp.linux-i686-2.7/bson/buffer.o -o build/lib.linux-i686-2.7/pymongo/_cmessage.so
Running setup.py install for anyjson
Successfully installed mongokit pymongo anyjson
Cleaning up...
Source: (StackOverflow)
I am using MongoKit (v0.8.1) and am bit confused about the behaviour when defining a document with use_schemaless=True
and use_dot_notation=True
. MongoKit ignores any dynamic attributes (ie. anything that hasn't been defined in structure dict) when saving to the db. Works as expected when accessing the document via the usual square brackets. Obviously, I can work around this by overriding __setattr__
but I find it a bit unexpected. Or is this intentional? Thanks!
Source: (StackOverflow)
I am following this http://pythonhosted.org/Flask-MongoKit/ as an example
I am just trying to get an instance of Document for writing unit test, but it isn't working. Here's the code for test:
import unittest
from tests import app, db, ctx
from word.models import Word
class ModelWordTestCase(unittest.TestCase):
def setUp(self):
pass
def test_model_word(self):
print db.Word
word = db.Word()
self.assertIsNotNone(word)
def tearDown(self):
pass
Word Class
from flask.ext.mongokit import Document
from core import db
@db.register
class Word(Document):
__collection__ = 'words'
use_dot_notation = True
STATUS = {
"approved" : 1,
"pending" : 0,
"rejected" : -1,
}
structure = {
'lang': unicode,
'local': unicode,
'pronunciation' : unicode,
'meaning': unicode,
}
Surprisingly db.Word exists when printed with print db.Word
statement, but it can't be called to create a new instance as done in tutorial I mentioned above. Here's the output of test:
Collection(Database(MongoClient('localhost', 27017), u'words_test'), u'Word')
E
======================================================================
ERROR: test_model_word (tests.model_tests.ModelWordTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/model_tests.py", line 14, in test_model_word
word = db.Word()
File "/usr/local/lib/python2.7/dist-packages/mongokit/collection.py", line 64, in __call__
self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'Word' method on a 'Database' object it is failing because no such method exists.
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (errors=1)
How can I fix this and get instance of Word document so that I can create and save a record.
Source: (StackOverflow)
When I make a query in Mongodb using Mongokit in Python, it returns a json document object. However I need to use the return value as a model type that I have defined. For example, if I have the class:
class User(Document):
structure = {
'name': basestring
}
and make the query
user = db.users.find_one({'name':'Mike'})
I want user to be an object of type User, so that I can embed it into other objects that have fields of type User. However it just returns a json document. Is there a way to cast it or something? This seems like something that should be very intuitive and easy to do.
Source: (StackOverflow)
I have documents like:
{
name: 'Nicholas',
friends : ['Amy', 'Joe', 'Amanda']
},
{
name: 'John',
friends : ['Amy', 'Amanda', 'Sam', 'Steve']
},
and I'd like to find all documents where 'Joe' is in the friends array.
In Mongo I guess this would be:
db.people.find({ 'friends' : { $elemMatch : 'Joe' } }
but how do I do this in Python with MongoKit:
connection.People.find( ?? )
Source: (StackOverflow)
I am a beginner and I have a simple application I have developed locally which uses mongodb with mongoKit as follows:
app = Flask(__name__)
app.config.from_object(__name__)
customerDB = MongoKit(app)
customerDB.register([CustomerModel])
then in views I just use the CustomerDB
I have put everything on heroku cloud but my database connection doesn't work.
I got the link I need to connect by:
heroku config | grep MONGOLAB_URI
but I am not sure how to pull this. I looked at the following post, but I am more confused
How can I use the mongolab add-on to Heroku from python?
Any help would be appreciated.
Thanks!
Source: (StackOverflow)
Good day dear colleagues, I decided to move some projects from MySQL to MongoDB and faced several difficulties:
For example there are two tables in MySQL:
Users:
CREATE TABLE `testdb`.`users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 55 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL
) ENGINE = MYISAM
Rules:
CREATE TABLE `testdb`.`rules` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`uid` INT NOT NULL ,
`title` VARCHAR( 155 ) NOT NULL ,
`points` INT NOT NULL
) ENGINE = MYISAM
Now to chose all "rules", which belong to a paticular user I can make SQL request:
SELECT r.`title`, r.`points` FROM `rules` r, `users` u WHERE r.`uid` = u.`id` AND u.`id` = '123'
By now, I can't figure out how to do the same in MongoDB, can you please explain and provide an example.
P.S. I make implementation in Python with the help of pymongo
P.P.S. I also wanted to see the alternative ways of solving this problem with the help of ORM mongoengine or mongokit.
Thank you in advance:)
Source: (StackOverflow)
I'm trying to use mongokit in my Pyramid project, but when I import it, I get the following error.
File "foo.py", line 5, in <module>
import mongokit
File "/home/.../mongokit/__init__.py", line 33, in <module>
from cursor import Cursor
ImportError: No module named 'cursor'
There is a cursor.py
file with a Cursor
class in it in the same folder as __init__.py
.
According to the documentation, when you import a module, it is supposed to put its own directory at the beginning of the search path. However, when I print out sys.path
from both foo.py
and __init__.py
, it is the same in both places.
I can't find anyone else having this problem.
Edit: I am using Python 3.3
Source: (StackOverflow)
I would like to remove all items from a test collection. My setup is
connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])
db = connection.test_database.tutorial
I have a document model class Test which maps to the tests collection. I've tried deleting the collection with both
connection.test_database.drop_collection('tutorial.tests')
db.tests.remove()
However querying something like
list(db.Test.find())
still gives me the old data. Something like
list(db.tests.find())
returns an empty list. However if I add new entries into tests the previous query also doesn't reflect the changes, so I don't think thats accurate either.
Source: (StackOverflow)