我能够连接到MongoDB并能够成功运行以下命令
mongo -host hostname:port
db.getSiblingDB("$external").auth({ mechanism: "PLAIN", user: "<username>", pwd: "<password>", digestPassword: false })
use dbname
db.collectionanme.find({query here})
我尝试使用ruby的相同命令,但我收到错误消息&gt;&gt;未授权对dbname.collectionname(13)
进行查询以下是红宝石代码:
require 'mongo'
include Mongo
client = Mongo::Client.new(['localhost:port'])
client.with(:auth_mech => :plain, :user => '<user>', :password => '<password>', :digestPassword => false)
db = client.use('dbname')
db[:collectionname].find(query here).each do |doc|
puts doc
end
这是错误
C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/operation/result.rb:214:in `validate!': not authorized for query on dbname.collectionname (13) (Mongo::Error::OperationFailure)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/operation/read/query.rb:62:in `block in execute_message'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/server/connection_pool.rb:99:in `with_connection'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/server/context.rb:63:in `with_connection'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/operation/read/query.rb:61:in `execute_message'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/operation/read/query.rb:55:in `execute'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/collection/view.rb:164:in `send_initial_query'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/mongo-2.0.6/lib/mongo/collection/view/iterable.rb:39:in `each'
答案 0 :(得分:0)
由于某些访问问题,我无法完成工作。但代码如下所示
require 'mongo'
include Mongo
Mongo::Client.new(['hostname:port'], :auth_mech => :plain, :auth_source => :external, :user => 'username', :password => 'password', :digestPassword => false).use('db_name')['collection_name'].find("query").to_a