小MongoDB对象层。
bearfield的Python项目详细描述
bearfield
==
一个小型、高效、易用的[mongodb][1]对象层,建立在[pymongo][2]之上。
[![构建状态](https://travis-ci.org/wifast/bearfield.svg?branch=master)(https://travis ci.org/wifast/bearfield)
功能
----
bearfield有一些有趣的功能:
-惰性字段解码。抓取文档很快,因为bearfield在您抓取文档之前不会对它们做任何操作。
-简单的字段声明:`value=field(str)`
-子文档也很简单:`subdoc=field(mydocument)`
-所有字段类型:document、date、time、datetime、list、set、dict和所有内置
-查询链:`q({'type':'grizzly'})q({'type':'kodiak'})`
-多个连接和数据库。
/>现在创建一个与我们的示例连接相关联的文档类型:
from bear field import document,field
class bear(document):
class meta:
connection='example'
type=field(str)
height=field(float)
相关选项包括"connection"(定义存储文档的命名连接)和"collection"(如果未提供,则默认为snake cased document类名)。为了向前
兼容性,忽略了其他选项。
使用该类型的对象很容易。让我们做一个9.8英尺的灰熊:
bear=bear(name='timmy',type='grizzly',height='9.8')
bear.save()
这将在"bear"对象上设置"id"字段。现在让我们去寻找熊:
bears=bear.find({'type':'grizzly'})
寻找熊中的熊:
print("这只灰熊有{}英尺高!".format(bear.height))
.format(bear.height))
"find_one"方法在找不到熊时不会引发异常。相反,它将
返回"none"。这很好,因为bears不喜欢异常。
"update"方法只更新文档上已更改的字段。这比一次更新整个文档的save更有效。因此,当我们的熊长大时,就会发生这种情况:
bear=bear.find乇one({"u id":bear乇identifier})
bear.height=10.3
bear.update()
首先从数据库中检索对象:
old_bear=bear.find_and_modify({''u id':bear_identifier},{'height':10.3})
print("我的熊以前是{}英尺高!但他现在更大了!".format(old_bear.height))
请注意,"find_and_modify"返回对象的旧值。这一点很重要,因为您在调用"find庠and庠modify"之前没有必要玩这个游戏。
子文档呢?让我们定义一个beartype并重新定义我们的bear文档来使用它:
class beartype(document):
name=field(str)
avg_height=field(int)
colors=field({str})
class bear(document):
class meta:
connection='e例如,'
name=field(str)
type=field(beartype)
height=field(float)
beartype不需要meta类,因为它与集合没有关联。使用它还是很容易的:
grizzly=bear type(name='grizzly',avg撸height=9.3,colors={'brown'})
bear=bear(name='timmy',type=grizzly,height=10.3)
我们可以使用引用使访问相关类型变得容易。引用与
文档模型关联,可以存储objectid或查询。我们将重新定义文档如下:
from bear field import reference
class beartype(document):
class meta:
connection='test'
name=field(str)
avg_height=field(int)
colors=field({str})
class bear(document):
class meta:
连接='示例'
name=field(str)
type=reference(beartype)
height=field(float)
唯一的区别是,grizzly类型需要保存在
数据库中,然后才能在bear文档上设置它:
grizzly.save()
bear=bear(name='timmy',type=grizzly,height=10.3)
E物体。引用的设计方式是这样的
,这样用户就不会因为执行不知道的查询而激怒任何熊。
type=bear.type.find_one()
我们可以按名称查询以完成上述
操作:
bear.type={name':'grizzly'}
type=bear.type.find_one()
包含的[许可证][3]文件。
[1]:http://www.mongodb.org/"mongodb"
[2]:http://api.mongodb.org/python/current/"pymongo"
[3]:https://github.com/wifast/bearfield/blob/master/license"许可证"
==
一个小型、高效、易用的[mongodb][1]对象层,建立在[pymongo][2]之上。
[![构建状态](https://travis-ci.org/wifast/bearfield.svg?branch=master)(https://travis ci.org/wifast/bearfield)
功能
----
bearfield有一些有趣的功能:
-惰性字段解码。抓取文档很快,因为bearfield在您抓取文档之前不会对它们做任何操作。
-简单的字段声明:`value=field(str)`
-子文档也很简单:`subdoc=field(mydocument)`
-所有字段类型:document、date、time、datetime、list、set、dict和所有内置
-查询链:`q({'type':'grizzly'})q({'type':'kodiak'})`
-多个连接和数据库。
/>现在创建一个与我们的示例连接相关联的文档类型:
from bear field import document,field
class bear(document):
class meta:
connection='example'
height=field(float)
相关选项包括"connection"(定义存储文档的命名连接)和"collection"(如果未提供,则默认为snake cased document类名)。为了向前
兼容性,忽略了其他选项。
使用该类型的对象很容易。让我们做一个9.8英尺的灰熊:
bear=bear(name='timmy',type='grizzly',height='9.8')
bear.save()
这将在"bear"对象上设置"id"字段。现在让我们去寻找熊:
bears=bear.find({'type':'grizzly'})
寻找熊中的熊:
print("这只灰熊有{}英尺高!".format(bear.height))
.format(bear.height))
"find_one"方法在找不到熊时不会引发异常。相反,它将
返回"none"。这很好,因为bears不喜欢异常。
"update"方法只更新文档上已更改的字段。这比一次更新整个文档的save更有效。因此,当我们的熊长大时,就会发生这种情况:
bear=bear.find乇one({"u id":bear乇identifier})
bear.height=10.3
bear.update()
首先从数据库中检索对象:
old_bear=bear.find_and_modify({''u id':bear_identifier},{'height':10.3})
print("我的熊以前是{}英尺高!但他现在更大了!".format(old_bear.height))
请注意,"find_and_modify"返回对象的旧值。这一点很重要,因为您在调用"find庠and庠modify"之前没有必要玩这个游戏。
子文档呢?让我们定义一个beartype并重新定义我们的bear文档来使用它:
class beartype(document):
name=field(str)
avg_height=field(int)
colors=field({str})
class bear(document):
class meta:
connection='e例如,'
name=field(str)
type=field(beartype)
height=field(float)
beartype不需要meta类,因为它与集合没有关联。使用它还是很容易的:
grizzly=bear type(name='grizzly',avg撸height=9.3,colors={'brown'})
bear=bear(name='timmy',type=grizzly,height=10.3)
我们可以使用引用使访问相关类型变得容易。引用与
文档模型关联,可以存储objectid或查询。我们将重新定义文档如下:
from bear field import reference
class beartype(document):
class meta:
connection='test'
name=field(str)
avg_height=field(int)
colors=field({str})
class bear(document):
class meta:
连接='示例'
name=field(str)
type=reference(beartype)
height=field(float)
唯一的区别是,grizzly类型需要保存在
数据库中,然后才能在bear文档上设置它:
bear=bear(name='timmy',type=grizzly,height=10.3)
E物体。引用的设计方式是这样的
,这样用户就不会因为执行不知道的查询而激怒任何熊。
type=bear.type.find_one()
我们可以按名称查询以完成上述
操作:
bear.type={name':'grizzly'}
type=bear.type.find_one()
包含的[许可证][3]文件。
[1]:http://www.mongodb.org/"mongodb"
[2]:http://api.mongodb.org/python/current/"pymongo"
[3]:https://github.com/wifast/bearfield/blob/master/license"许可证"