MongoDB的,web2py的DAL可以是這樣的
db = DAL('mongodb://username:[email protected]/test', pool_size=0)
db.define_table('address',
Field('street'),
Field('city'),
Field('state'),
Field('zip'),
format='%(name)s')
db.define_table('table',
Field('name'),
Field('address','list:reference address'),
format='%(name)s')
對於插入你可以做
first = db.address.insert(street = '123 Fake Street',
city = 'Faketon',
state = 'MA',
zip = '12345')
second = db.address.insert(street = '1 Some Other Street',
city = 'Boston',
state = 'MA',
zip = '12345')
db.table.insert(name = 'Joe Bookreader', address = [first, second])
或者直接就可以做到這一點代碼:
db.define_table('table_name',Field('name'),Field('addresses','text'))
list_data = [
{
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: 12345
},
{
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: 12345
}
]
db.table_name.insert(name='Joe Bookreader', addresses=list_data)
沒有Web2pyDAL,MongoDB的工作更好,它非常簡單。只有當你需要交叉platofrm數據庫語法時,你需要DAL。不知道如果身份驗證如何在web2py + mongo tho中工作,還沒有更新很久。 –