如何将此mongodb shell表达式转换为ruby mongo驱动程序

如何将此mongodb shell表达式转换为ruby mongo驱动程序,ruby,mongodb,driver,Ruby,Mongodb,Driver,我有这个mongodbshell命令 db.districts.find({servers:{$exists:true}}, {name:1}) 我很难翻译成ruby mongo驱动程序语法 coll = db.collection('districts') coll.find({"servers"=>{"$exists"=>true}}, {'name'=>1}).to_a 但它抱怨错误 ***RuntimeError异常:未知选项[{name=>1}]查询语法如下所示

我有这个mongodbshell命令

db.districts.find({servers:{$exists:true}}, {name:1})
我很难翻译成ruby mongo驱动程序语法

coll = db.collection('districts')
coll.find({"servers"=>{"$exists"=>true}}, {'name'=>1}).to_a
但它抱怨错误
***RuntimeError异常:未知选项[{name=>1}]

查询语法如下所示

collection.find(selector = {}, opts = {})
而查询应为—

collection.find({
  "servers" => {
    "$exists" => true
  }
}, {: fields => {
    "_id" => 0, "name" => 1
  }
})

我没有用$exists测试它,但应该可以用。

是的,我在谷歌搜索了一段时间后得到了答案。使用以下适用于我的选择器={'servers'=>{'$exists'=>true}}opt={:fields=>[servers]}