码迷,mamicode.com
首页 > 数据库 > 详细

mongodb 实现关系型数据库中查询某一列 的效果

时间:2017-07-31 15:48:04      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:data-   sts   article   find   mod   mongo   ack   关系   ble   

近期在tornado\mongodb\ansible


mongodb中有个find()方法非常牛逼,能够将集合中全部的表都传出来,一開始我这么写


class Module_actionHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        coll = self.application.db.waitfish
        hosts = coll.find({}, {'hostname':1,"_id":0})
        modulenames = ['ping', 'setup', 'copy']
        self.render(
            "module_action.html",
            hosts = hosts,
            modulenames = modulenames,
        )

然后在模板中:

<select name="hostname">
                {% for host in hosts %}
                <option name="{{ host['hostname'] }}" >{{ host['hostname'] }}</option>
                {% end %}
</select>

后来想了想find()方法是找出全部的表,这太伤不起了..

于是又去看了度娘,基本上都是列出一大堆查询命令,好多中方法的.

最后我还是选了这一种:

class Module_actionHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        coll = self.application.db.waitfish
        hosts = coll.find({}, {'hostname':1,"_id":0})#解释一下,这里hostname:1 表示返回hostname列,因为_id列每次都返回所以用0禁用掉,模板还一样
        modulenames = ['ping', 'setup', 'copy']
        self.render(
            "module_action.html",
            hosts = hosts,
            modulenames = modulenames,
        )


mongodb 实现关系型数据库中查询某一列 的效果

标签:data-   sts   article   find   mod   mongo   ack   关系   ble   

原文地址:http://www.cnblogs.com/slgkaifa/p/7263043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!