2013-03-18 76 views
0

有人可以幫我嗎?我試圖簡單地檢索字段「地址」中的內容。我無法得到它的打印速度比空括號以外的任何「[]」Empty EmbeddedDocument字段?

下面是一個例子紀錄:

{ 
    "_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) 

和循環應打印嵌入式文檔,但僅爲每個對象返回一個[]。

for building in Buildings.objects: 
    print building.addresses 

任何想法,爲什麼這不起作用?

回答

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'], 
    } 

要存儲記錄as:

{ 
    "_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

EmbeddedDocumentFieldReferenceFiled可以得到參數爲EmbeddedDocumentDocument類或字符串('self'自我參考)。

如果你想讓id作爲對象,你可能必須在代碼中使用它。

如果你想要另一種日期時間格式可能你必須使用ComplexDateTimeField或實施它與基類ComplexDateTimeField

+0

我不確定這適用於我...我不確定發生了什麼事。仍然打印一個空白[] – 2013-03-18 12:11:00

+0

你能幫我理解你的例子和我之間不起作用嗎? – 2013-03-18 12:18:56

+0

你可以打開mongo控制檯,並從你的集合'db.building.find()'中選擇任何文檔,因爲你的json例子不喜歡真正的mongo文檔(我不能將你的json插入到collention中作爲db.building .insert(your_json_there)')?在我的例子中,我使用'ObjectId' - 默認的mongo id,在你的代碼中你也使用了顯式的。但是你的json'_id'格式很奇怪。我認爲你有id域的問題。 – tbicr 2013-03-18 13:44:48