Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert data by pymongo using mongoengine ORM in pyramid
    primarykey
    data
    text
    <p>I want to use pymongo connection and methods to work with mongodb, but at the same time i want using mongoengine ORM.</p> <p>Sample:</p> <pre><code>class User(Document): email = StringField(required=True) first_name = StringField(max_length=50) last_name = StringField(max_length=50) john = User(email='jonhd@example.com') john.first_name = 'Jonh' john.last_name = 'Hope' </code></pre> <p>And now i want to insert new formed document User into my 'test_collection'. In case using only mongoengine i can do this:</p> <pre><code>connect('test_database') john.save() </code></pre> <p>And then i can make easy accessing my data:</p> <pre><code>for user in User.objects: print user.first_name </code></pre> <p>But when i'm trying to do same using pymongo, i've got an error:</p> <pre><code>connection = MongoClient('localhost', 27017) db = connection.test_database collection = db.test_collection collection.insert(john) </code></pre> <p>Traceback:</p> <pre><code>Traceback (most recent call last): File "C:/Users/haribo/PycharmProjects/test/mongoeng.py", line 18, in &lt;module&gt; collection.insert(john) File "C:\Python27\lib\site-packages\pymongo\collection.py", line 353, in insert docs = [self.__database._fix_incoming(doc, self) for doc in docs] File "C:\Python27\lib\site-packages\pymongo\database.py", line 258, in _fix_incoming son = manipulator.transform_incoming(son, collection) File "C:\Python27\lib\site-packages\pymongo\son_manipulator.py", line 73, in transform_incoming son["_id"] = ObjectId() TypeError: 'str' object does not support item assignment </code></pre> <p>But this works:</p> <pre><code>connection = MongoClient('localhost', 27017) db = connection.test_database collection = db.test_collection john = { 'email': 'jonhd@example.com', 'first_name': 'Jonh', 'last_name': 'Hope' } collections.insert(john) </code></pre> <p>And accessing data using pymongo:</p> <pre><code>print collection.find_one() {u'last_name': u'Hope', u'first_name': u'Jonh', u'_id': ObjectId('513d93a3ee1dc61390373640'), u'email': u'jonhd@example.com'} </code></pre> <p>So, my main idea and question is:</p> <p>How i can using mongoengine for ORM and pymongo connection and methods to working with mongodb?</p> <p>P.S. I mentioned that i want use it in pyramid for some context.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload