MongoEngine/mongoengine

List Comprehensions and Document.objects does not work

hebertodelrio opened this issue · 2 comments

Given the following document definition

class Chromatogram(Document):
fname = StringField()
liquor = StringField()
producer = StringField()
sample = StringField()
time = ListField(FloatField())
absorbance = ListField(FloatField())

def to_pandas(self):
    return pd.DataFrame(dict(fname=c.fname, liquor=c.liquor, producer=c.producer, sample=c.sample, time=c.time, absorbance=c.absorbance))

Why is the following two pieces code do not produce the same result

df1 = pd.concat([c.to_pandas() for c in Chromatogram.objects])

cc = []
for c in Chromatogram.objects:
cc.append(c.to_pandas())
df2 = pd.concat(cc)

df2 contains the right results, but df1 only contains one single chromatogram. For df1, I have 'rewinded' the query but in any case it only returns the last document in the database.

I havn't debug your problem but MongoEngine has no problem with list comprehension as demonstrated in the example below

from mongoengine import *

connect()

class Chromatogram(Document):
    fname = StringField()
    
Chromatogram(fname='test1').save()
Chromatogram(fname='test2').save()

print(Chromatogram.objects.count())    # 2

df1 = [c for c in Chromatogram.objects]
len(df1)    # 2

cc = []
for c in Chromatogram.objects:
    cc.append(c)
print(len(cc))    # 2

So problem is either your to_pandas or the pd.concat ...

Thank you so much for the reply, you were completely right. The issue was my to_pandas. fixed it.