浅析mongoEngine的document对象
引言:
from mongoengine import *
connect('local')
class Test(Document):
name=StringField(max_length=32) t = Test(name='Tommy.Yu')
方法 | 描述 | ||
DoesNotExist | None | ||
MultipleObjectsReturned | None | ||
cascade_save |
Recursively saves any references / generic references on an objects 顺藤摸瓜式的保存所有被对象引用到的数据。就是保存EmbedDocument这种数据以及外键关联的数据。对象本身不做保存。如下: >>> t2= Test(name='Joe') |
||
clean | Hook for doing document level data cleaning before validation is run. Any ValidationError raised by this method will not be associated with a particular field; it will have a special-case association with the field defined by NON_FIELD_ERRORS. | ||
compare_indexes |
Compares the indexes defined in MongoEngine with the ones existing in the database. Returns any missing/extra indexes. 对比mongoengine和数据库的索引。返回缺失/多余的索引。和ensure_index(es)配合使用。 >>> t.ensure_index('name') |
||
delete |
Delete the :class:`~mongoengine.Document` from the database. This will only take effect if the document has been previously saved. :param write_concern: Extra keyword arguments are passed down which will be used as options for the resultant ``getLastError`` command. For example, ``save(..., write_concern={w: 2, fsync: True}, ...)`` will wait until at least two servers have recorded the write and will force an fsync on the primary server. 从数据库中删除mongoengine.Document实例。 之前调用了save方法才起作用。 参数:write_concern: ... 例如, save(..., write_concern={w:2, fsync:True},...) 的实际调用的时机: 至少有两个服务器执行了写操作,将会迫使主服务器执行fsync(同步)。 >>> t2.delete() |
||
drop_collection |
Drops the entire collection associated with this :class:`~mongoengine.Document` type from the database. 删除和mongoengine.Document子类关联的表(collection)。 >>> t.drop_collection() |
||
ensure_index |
Ensure that the given indexes are in place. :param key_or_list: a single index key or a list of index keys (to construct a multi-field index); keys may be prefixed with a **+** or a **-** to determine the index ordering 在mongoenging中加入索引。影响的是整个类的,不是实例的。直到整个collection被删除(drop_collection被调用)都有效。 >>> t3=Test(name='John') |
||
ensure_indexes |
Checks the document meta data and ensures all the indexes exist. Global defaults can be set in the meta - see :doc:`guide/defining-documents` .. note:: You can disable automatic index creation by setting `auto_create_index` to False in the documents meta data 检查document的meta信息,并且确保所有的索引都存在于(mongoengine/db?) >>> class Test2(Document): 实在是看不出来,看看数据库(经过测试,证实在save的时候已经创建了索引。索引对于性能的提升很夸张,看这里。): > db.test2.getIndexes() 无法确定这个函数干嘛了。 |
||
from_json |
将json数据转化为未保存的documeng对象。 >>> b= t.from_json('{"name":"joe"}') |
||
list_indexes |
Lists all of the indexes that should be created for given collection. It includes all the indexes from super- and sub-classes. 列举出表(collection)的所有索引。包含父类和子类的! >>> t.list_indexes() |
||
my_metaclass | Metaclass for top-level documents (i.e. documents that have their own collection in the database. | ||
register_delete_rule | This method registers the delete rules to apply when removing this object. | ||
reload |
从数据库重新加载所有属性 >>> a=t.reload() |
||
save |
>>>u.save() 源码在这里。 保存 Document到数据库. 存在则修改,否则插入。
|
||
select_related |
Handles dereferencing of :class:`~bson.dbref.DBRef` objects to a maximum depth in order to cut down the number queries to mongodb. .. versionadded:: 0.5 >>> t.select_related() |
||
switch_collection | Temporarily switch the collection for a document instance. Only really useful for archiving off data and calling `save()`:: user = User.objects.get(id=user_id) user.switch_collection('old-users') user.save() If you need to read from another database see :class:`~mongoengine.context_managers.switch_db` :param collection_name: The database alias to use for saving the document | ||
switch_db | Temporarily switch the database for a document instance. Only really useful for archiving off data and calling `save()`:: user = User.objects.get(id=user_id) user.switch_db('archive-db') user.save() If you need to read from another database see :class:`~mongoengine.context_managers.switch_db` :param db_alias: The database alias to use for saving the document | ||
to_dbref |
Returns an instance of :class:`~bson.dbref.DBRef` useful in `__raw__` queries. 返回bson.dbref.DBRef的实例。在'__raw__'查询时比较有用(pyMongo?) >>>t.to_dbref() |
||
to_json |
转换成json对象 >>>t.to_json() |
||
to_mongo |
Return as SON data ready for use with MongoDB. >>> t.to_mongo() |
||
update |
Performs an update on the :class:`~mongoengine.Document` A convenience wrapper to :meth:`~mongoengine.QuerySet.update`. Raises :class:`OperationError` if called on an object that has not yet been saved. 在mongoengine.Document类上进行更新操作。方法 mongoengine.QuerySet.update的简单包装。如果对象尚未调用save方法,会触发OperationError异常。 ps:源码。加入upsert参数是删除所有字段 >>> t.to_json() 更新字段需要在栏位前面加入set__,如下: >>> t.update(set__name='joe')
|
||
validate | Ensure that all fields' values are valid and that required fields are present. |
浅析mongoEngine的document对象的更多相关文章
- 报表软件JS开发引用HTML DOM的location和document对象
上一次提到,在报表软件FineReport的JavaScript开发中,可以访问并处理的HTML DOM对象有windows.location.document三种.这次就继续介绍后两种,locati ...
- Document对象和window对象
window对象--- 代表浏览器中的一个打开的窗口或者框架,window对象会在<body>或者<frameset>每次出现时被自动创建,在客户端JavaScript中,Wi ...
- Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- JavaScript的DOM操作。Window.document对象
间隔执行一段代码:window.setlnteval("需要执行的代码",间隔毫秒数) 例 : window.setlnteval("alert("你 ...
- HTML DOM Document 对象
HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM (Document Object Model) 中 , 每一个元素都是 节点: 文档是一个文档. 所有的HTML ...
- JS中document对象和window对象有什么区别
简单来说,document是window的一个对象属性.Window 对象表示浏览器中打开的窗口.如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 windo ...
- 9.22 window对象、document对象
一.window对象: 属性(值或者子对象): opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null,可以利用这个属性来关闭源窗口 dialogArgume ...
- Window.document对象 轮播练习
Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docun ...
- javascript获取iframe框架中页面document对象,获取子页面里面的内容,iframe获取父页面的元素,
javascript获取iframe框架中,加载的页面document对象 因为浏览器安全限制,对跨域访问的页面,其document对象无法读取.设置属性 function getDocument(i ...
随机推荐
- Sort 整理
文章.图片参考:http://www.jianshu.com/p/1b4068ccd505?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sour ...
- web页面的加载顺序
1.页面顺序 一个典型的web页面由于三个部分组成:html.css和JS.执行的顺序是: 在构造完HTML的dom结构时.触发DOMContentLoaded事件. 整个执行过程安装html的顺序来 ...
- sql之truncate 、delete与drop区别
sql之truncate .delete与drop区别相同点:truncate 和不带 where 子句的 delete,以及 drop 一定会删除表内的数据不同点:1. truncate 和 del ...
- chain.doFilter(request,response)含义
过滤器的生命周期一般都要经过下面三个阶段: 初始化 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上并 ...
- salt stack 工具之一——远程命令
salt stack 远程命令 salt stack是一种自动化的运维工具,可以同时对N台服务器进行配置管理.远程命令执行等操作. salt stack分为两个部分: salt-master,部署在控 ...
- MFS文件系统
一.MFS文件系统概论 MFS是linux下的开源存储系统,是由波兰人开发的.MFS文件系统能够实现RAID的功能,不但能够节约存储成本,而且不逊于专业的存储系统,能够实现在线扩展.MFS是一种半分布 ...
- 代码重构 & 常用设计模式
代码重构 重构目的 相同的代码最好只出现一次 主次方法 主方法 只包含实现完整逻辑的子方法 思维清楚,便于阅读 次方法 实现具体逻辑功能 测试通过后,后续几乎不用维护 重构的步骤 1 新建一个方法 ...
- PHP中spl_autoload_register()函数
spl_autoload_register — 注册给定的函数作为 __autoload 的实现 官方地址:http://php.net/manual/zh/function.spl-autoload ...
- jQuery回调、递延对象总结(上篇)—— jQuery.Callbacks
前言: 作为参数传递给另一个函数执行的函数我们称为回调函数,那么该回调又是否是异步的呢,何谓异步,如:作为事件处理器,或作为参数传递给 (setTimeout,setInterval)这样的异步函数, ...
- jQuery源码-class操作
写在前面 本文写作基于jQuery 1.9.1版本,源码分析系列目录:http://www.cnblogs.com/chyingp/archive/2013/06/03/jquery-souce-co ...