Reputation: 697
I am working with a mongo database model building using mongoengine. I want to modify DictField
by the result created in another function
from flask_mongoengine import MongoEngine
import marshmallow_mongoengine as ma
db = MongoEngine()
class TestData(db.Document):
name = db.StringField(max_length=50, required=True,unique=True)
num1 = db.IntField()
num2 = db.IntField()
ml_proc = db.DictField()
hum_proc = db.DictField()
class TestDataSchema(ma.ModelSchema):
class Meta:
model = TestData
The sample data in database will be like below
Sample Data
[{
"name" : "david",
"num1" : 5,
"num2" : 4,
"ml_proc": {"sm": 7, "prod":12}
},
{
"name" : "john",
"num1" : 1,
"num2" : 4
},
{
"name" : "dune",
"num1" : 5,
"num2" : 5,
"ml_proc": {"sm": 7, "prod":15}
},
{
"name" : "jan",
"num1" : 3,
"num2" : 4
}]
Out of 4 documents in sample data, 2nd and 4th document have missing field ml_proc
. The fields in ml_proc
are sm
and prod
which are created from fields num1
and num2
calculated in another function defined as
import pandas as pd
def calc(data):
data["sm"] = data["num1"].apply(lambda x : x+2)
data["prod"] = data["num2"].apply(lambda x : x*2)
return data
I know its weird to think why this function is necessary but this is a very simple one for my requirement. The number of documents I am handling will be thousands at a time. Thats why i used pandas dataframe for processing. So my doubt is how to update Dictfield in multiple documents using result generated from pandas data processing
Any help is appreciated, Thanks in advance
Database Note: Dont take too much conc on values. This is just a sample image.
Upvotes: 1
Views: 119
Reputation: 10737
Maybe you need something like this:
db.collection.update({
"ml_proc": {
$exists: false
}
},
[
{
$addFields: {
ml_proc: {
sm: {
$sum: [
"$num1",
2
]
},
prod: {
$sum: [
"$num2",
2
]
}
}
}
}
],
{
multi: true
})
Explained:
Upvotes: 1