【问题标题】:how to translate this mongodb shell expression into ruby mongo driver如何将此 mongodb shell 表达式转换为 ruby​​ mongo 驱动程序
【发布时间】:2015-05-16 00:15:30
【问题描述】:

我有这个 mongodb shell 命令

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}]

【问题讨论】:

    标签: ruby mongodb driver


    【解决方案1】:

    查询语法应如下所示

    collection.find(selector = {}, opts = {})
    

    并且查询应该是 -

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

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

    【讨论】:

    • 是的,我在谷歌搜索后得到了答案。使用以下对我有用的选择器 = {'servers' => {'$exists' => true}} opt={:fields => ["servers"]}
    猜你喜欢
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 2010-12-21
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多