Skip to content

Mongoengine: Save not working - but PyMongo insert_one() works on same model #2693

@mg3146

Description

@mg3146

I am having trouble getting mongoengine's .save() to actually save a new document, but I'm able to save the same document via pymongo.insert_one(). My model class is.

class BlotterItem(me.Document):    
    symbol = me.StringField(required = True)
    price = me.EmbeddedDocumentField(Price, required = True)
    trade_date = me.DateTimeField(required = True)
    exchange = me.StringField(max_length=120, required=True)
    tags = me.EmbeddedDocumentField(Strategy_Tags, required = True)
   
    # Document-Admin Fields
    last_update_date = me.DateTimeField()
    trade_id = me.StringField(required = True)
    document_version = me.IntField(default=1)
    
    # mongoengine stuff:
    meta = {'db_alias' : 'db1',
            'collection' : 'Blotter',
            'allow_inheritance': True}

In the interpreter, I would then try:

import mongoengine as me
me.connect(host = <conn_string>, alias = 'db1')

item = BlotterItem(<lots of stuff>)
item.save()   # <-- Checking the database, there is nothing there. So I try force_insert..

item.save(force_insert = True)   #<-- This throws the below error, even though nothing in the DB...

NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error collection: test.Blotter index: _id_ dup key: { _id: ObjectId('63401b7e82967e682f0714e0') }

To see if I'm crazy, I then used PyMongo...

*skipping my code that gives me the PyMongo collection object*
collection.insert_one(item.to_mongo())    # <----- Works!!

So to me it doesn't seem like it's an issue with BlotterItem model because inserting via the .to_mongo() method works...

Any help is appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions