我正在尝试使用 ruby mongo 驱动程序将哈希插入 mongodb。我明白了undefined method has_key? error
下面是代码
require 'rubygems'
require 'mongo'
include Mongo
@client = MongoClient.new('localhost', 27017)
@db = @client['sample-db']
@coll = @db['test1']
@coll.remove
puts abc
#value of abc hash is {:command=>"runlocal", :remotecommand=>"rm process_2013-04-25.log"}
@coll.insert("#{abc}")
输出应该看起来像
{ "_id" : ObjectId("5174c4aeb2441139ec000001"), "command" : "runlocal", "remotecommand" : "rm......" }
查看论坛后,我找到了如下解决方案。但我不想更改架构。
id = @coll.insert({:status =>"#{abc}"})
哪个生产
{ "_id" : ObjectId("517b69d2b2441136b8000001"), "status" : "{'command' => "runlocal"....
更新:解决方案
id = @coll.insert({"#{abc}"})
根据需要产生以下输出
{"_id"=>BSON::ObjectId('517bfc79b244110a08000001'), "command" : "runlocal", "remotecommand" : "rm process_2013-04-25.log"}