0

有人能帮助我吗?我试图简单地检索“地址”字段中的内容。除了空括号“[]”之外,我无法打印任何内容

这是一个示例记录:

{
  "_id": { "$oid" : "51376F833EF9EB361F354ED0" },
  "addresses": [
    {
      "city": "CAMBRIDGE",
      "suffix": "",
      "street_name": "FRANKLIN ST",
      "prefix": "",
      "zipcode": "49503",
      "state": "MA",
      "street_num": "127"
    }
  ],
  "created_at":{ "$date": 1361160419000.000000 }
}

这是我的课程代码:

class Buildings(db.Document):
    _id = db.ObjectIdField(required=True)
    addresses = db.ListField(db.EmbeddedDocumentField('Address'))
    created_at = db.DateTimeField(default=datetime.datetime.now, required=False)
    meta = {
        'allow_inheritance': False,
        'indexes': ['_id'],
        'ordering': ['-created_at']
    }
class Address(db.EmbeddedDocument):
   city = db.StringField(required=False)
   suffix = db.StringField(required=False)
   street_name = db.StringField(required=False)
   zipcode = db.StringField(required=False)
   prefix = db.StringField(required=False)
   state = db.StringField(required=False)
   is_contracted = db.IntField(required=False)
   street_num = db.StringField(required=False)

还有一个循环应该打印嵌入的 Document 但只为每个对象返回一个 [] 。

for building in Buildings.objects:
    print building.addresses

知道为什么这不起作用吗?

4

1 回答 1

2

尝试检查您的代码如何在 mongodb 中存储记录:

address = Address(**{
    "city": "CAMBRIDGE",
    "suffix": "",
    "street_name": "FRANKLIN ST",
    "prefix": "",
    "zipcode": "49503",
    "state": "MA",
    "street_num": "127"
})
Buildings(_id=ObjectId(), addresses=[address]).save()

您会看到下一个结果:

{
    "_id": ObjectId("5146e17969d90d0f124536ee"),
    "addresses": [{
        "_types": ["Address" ],
        "city": "CAMBRIDGE",
        "suffix": "",
        "street_name": "FRANKLIN ST",
        "state": "MA",
        "zipcode": "49503",
        "prefix": "",
        "_cls": "Address",
        "street_num": "127"
    }],
    "created_at" : ISODate("2013-03-18T12:42:17.389Z")
}

我会将您的模型实现为:

class Address(db.EmbeddedDocument):
    city = db.StringField()
    suffix = db.StringField()
    street_name = db.StringField()
    zipcode = db.StringField()
    prefix = db.StringField()
    state = db.StringField()
    is_contracted = db.IntField()
    street_num = db.StringField()

    meta = {
        'allow_inheritance': False,
    }


class Buildings(db.Document):
    addresses = db.ListField(db.EmbeddedDocumentField(Address))
    created_at = db.DateTimeField(default=datetime.datetime.now)

    meta = {
        'allow_inheritance': False,
        'indexes': ['_id'],
        'ordering': ['-created_at'],
    }

将记录存储为:

{
    "_id": ObjectId("5146e2b069d90d0f2cc3911a"),
    "addresses": [{
        "city": "CAMBRIDGE",
        "suffix": "",
        "street_name": "FRANKLIN ST",
        "state": "MA",
        "zipcode": "49503",
        "prefix": "",
        "street_num": "127"
    }],
    "created_at": ISODate("2013-03-18T12:47:28.939Z")
}

我不知道您如何保存带有字段"_id": { "$oid": "51376F833EF9EB361F354ED0"}和的文档"created_at":{"$date": 1361160419000.000000},但我认为其中存在问题。

required字段参数默认为False.

EmbeddedDocumentField并且ReferenceFiled可以获取参数EmbeddedDocumentDocument类或字符串('self'供自己参考)。

如果您想将 id 作为对象,您可能还必须在代码中创建它。

如果您想要另一种日期时间格式,您可能必须使用ComplexDateTimeField或使用基类来实现它ComplexDateTimeField

于 2013-03-18T09:59:31.110 回答