免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2735 | 回复: 3

pymongo eval [复制链接]

论坛徽章:
0
发表于 2015-11-15 21:04 |显示全部楼层
请问 pymongo 模块中的eval怎么使用

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
发表于 2015-11-16 09:15 |显示全部楼层
回复 1# ws00310976
def eval(self, code, *args)的说明:

Evaluate a JavaScript expression in MongoDB.

Useful if you need to touch a lot of data lightly; in such a
scenario the network transfer of the data could be a
bottleneck. The `code` argument must be a JavaScript
function. Additional positional arguments will be passed to
that function when it is run on the server.

Raises :class:`TypeError` if `code` is not an instance of
:class:`basestring` (:class:`str` in python 3) or `Code`.
Raises :class:`~pymongo.errors.OperationFailure` if the eval
fails. Returns the result of the evaluation.

arameters:
  - `code`: string representation of JavaScript code to be
    evaluated
  - `args` (optional): additional positional arguments are
    passed to the `code` being evaluated

.. warning:: the eval command is deprecated in MongoDB 3.0 and
  will be removed in a future server version.

论坛徽章:
0
发表于 2015-11-16 21:55 |显示全部楼层
官方的例子,我看不太懂,能 举个实际的例子吗

论坛徽章:
19
酉鸡
日期:2015-02-16 11:13:06操作系统版块每周发帖之星
日期:2015-12-02 15:01:04每日论坛发贴之星
日期:2015-11-28 06:20:00IT运维版块每日发帖之星
日期:2015-11-28 06:20:00每日论坛发贴之星
日期:2015-11-27 06:20:00IT运维版块每日发帖之星
日期:2015-11-27 06:20:00IT运维版块每日发帖之星
日期:2015-11-20 06:20:00每日论坛发贴之星
日期:2015-11-19 06:20:00IT运维版块每日发帖之星
日期:2015-11-19 06:20:00每日论坛发贴之星
日期:2015-11-15 06:20:00数据库技术版块每日发帖之星
日期:2015-11-15 06:20:00程序设计版块每日发帖之星
日期:2015-11-15 06:20:00
发表于 2015-11-17 15:08 |显示全部楼层
def update(request):
    id = eval("request." + request.method + "['id']")
    post = Post.objects(id=id)[0]
   
    if request.method == 'POST':
        # update field values and save to mongo
        post.title = request.POST['title']
        post.last_update = datetime.datetime.now()
        post.content = request.POST['content']
        post.save()
        template = 'index.html'
        params = {'Posts': Post.objects}

    elif request.method == 'GET':
        template = 'update.html'
        params = {'post':post}
   
能已经注意到用来索引文档 ID 的 eval 语句。使用该语句是为了避免编写以下语句:
if request.method == 'POST':
        id = request.POST['id']
    elif request.method == 'GET':
        id = request.GET['id']
    return render_to_response(template, params, context_instance=RequestContext(request))
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP