2

I am trying to save a MongoEngine Document into a Dynamic Document. Sample code is as follows:

class Small(mongoengine.Document):
    var_1 = mongoengine.IntField(db_field="var_1")
    var_2 = mongoengine.IntField(db_field="var_2")

class Main(mongoengine.DynamicDocument):
    timeseries = mongoengine.ListField(mongoengine.EmbeddedDocumentField('Small'),
                                       db_field="timeseries")

    def sample_insert():
        input = [(2, 4), (2, 6), (3, None), (4, None)]

        to_insert = [Small(var_1=i[0], var_2=i[1]) for i in input]

        self.timeseries = to_insert
        self.save()

The problem I am having occurs when one or more of the values to be saved is None. In this case the following error is generated:

mongoengine.errors.ValidationError: ValidationError (Data:55c0d57d1853bcad5c17b1f7) (var_1.Field is required)

I could set the None values to 0, but this is not a good solution as I can no longer distinguish between legitimate 0 values and non-existing values.

What I would love to do is be able to insert these None values as a 'null' entry into MongoDB.

Is this possible?

1 Answer 1

0

I had no problem running your code. The document inserted looks like this:

"timeseries" : [{ "var_1" : 2, "var_2" : 4 }, { "var_1" : 2, "var_2" : 6 }, { "var_1" : 3 }, { "var_1" : 4 }]

Before I could've run the code, I had to put self as an argument of your sample_insert function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.