標籤:mongodb 查詢單列
最近在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 實現關係型資料庫中查詢某一列 的效果