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

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

WBOY
WBOYOriginal
2016-06-07 15:58:571375Durchsuche

最近在tornado\mongodb\ansible mongodb中有个find()方法很牛逼,可以将集合中所有的表都传出来,一开始我这么写 class Module_actionHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): coll = self.application.db.waitfish hosts = col

最近在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[&#39;hostname&#39;] }}" >{{ host[&#39;hostname&#39;] }}</option>
                {% end %}
</select>
后来想了想find()方法是找出所有的表,这太伤不起了..

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

最后我还是选了这一种:

class Module_actionHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        coll = self.application.db.waitfish
        hosts = coll.find({}, {&#39;hostname&#39;:1,"_id":0})#解释一下,这里hostname:1 表示返回hostname列,由于_id列每次都返回所以用0禁用掉,模板还一样
        modulenames = [&#39;ping&#39;, &#39;setup&#39;, &#39;copy&#39;]
        self.render(
            "module_action.html",
            hosts = hosts,
            modulenames = modulenames,
        )
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn