使用Mongoengine查询嵌套文档查询

时间:2015-07-10 15:32:58

标签: mongoengine

我一直在尝试用python和http://php.net/manual/en/function.fam-monitor-file.php查询一些mongo实例变量 我需要从所有Collection中的每个 RegistroPozo 中获取所有 Variable

示例代码:

from mongoengine import *
class Variable(EmbeddedDocument):
    mnem=StringField(required=True, max_length=200)
    description=StringField(max_length=500)
    unit=StringField( max_length=200,default='ppm')
    alias=StringField( max_length=200) #,default=mnem
    type=StringField( max_length=200,default='DEPENDANT')



class RegistroPozo(EmbeddedDocument):
    fecha = DateTimeField()
    filepath = StringField()
    start = FloatField()
    step = FloatField()
    stop = FloatField()
    variables = EmbeddedDocumentListField(Variable)
    registros = ListField(DictField())
    version_information_block = StringField(max_length=500)
    well_information_block = StringField(max_length=500)
    curve_information_block = StringField(max_length=500)
    parameter_information_block = StringField(max_length=500)
    other_block = StringField(max_length=500)


class Pozo(DynamicDocument):
  uwi_pozo = StringField(required=True, max_length=200, primary_key=True)
  nom_pozo = StringField(required=True, max_length=200)
  prof_total = FloatField(required=True)
  elev_terr = FloatField(required=True)
  long_pozo = FloatField(required=True)
  lat_pozo = FloatField(required=True)
  coord_x_po = FloatField(required=True)
  coord_y_po = FloatField(required=True)
  registros_pozo=EmbeddedDocumentListField(RegistroPozo)

当我尝试查询时,我会做很多循环,但我相信有更好的方法可以做到这一点。 类似的东西:

variables_in_all_the_doc = Pozo.objects(Q(AdvanceQuery))

1 个答案:

答案 0 :(得分:0)

Pozo是mongo db级别的单个文档。嵌入式文档只是mongoengine构造。因此,当您阅读Pozo对象时,所有嵌入的文档也可在同一查询中使用(请与mongostat一起查看)。现在迭代这些数据并不是一个大问题。对于高级EmbeddedDocumentList查询,请阅读文档here