Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Mongo DB具有多条相同值的记录_Ruby_Mongodb_Uuid - Fatal编程技术网

Ruby Mongo DB具有多条相同值的记录

Ruby Mongo DB具有多条相同值的记录,ruby,mongodb,uuid,Ruby,Mongodb,Uuid,我对MongoDB和数据库都是新手。我正在使用Ruby,我想查询数据库中的特定UUID ID存储为_ID,值为'101b437a-be16-44f6-b0b0-0201cdee6510' 我通常会查询我的数据库: field = '_id:' value = 101b437a-be16-44f6-b0b0-0201cdee6510 def query_field(field,value) query = {#{field}: value} @result = @mong

我对MongoDB和数据库都是新手。我正在使用Ruby,我想查询数据库中的特定UUID

ID存储为_ID,值为'101b437a-be16-44f6-b0b0-0201cdee6510'

我通常会查询我的数据库:

  field = '_id:'
  value =  101b437a-be16-44f6-b0b0-0201cdee6510

  def query_field(field,value)
  query = {#{field}: value}

  @result = @mongo_interface.get(query)
  expect(@result.count).to be >= 1
  puts "Number of matched values:  #{@result.count}"

end


def get(param_hash, collection_name = nil)
   col_name = (collection_name.nil? || collection_name.empty?) ? @collection : collection_name
@docs = @db[col_name].find(param_hash)
结束

当我查看_id字段时,我假设它存储为某种二进制键,因此在搜索时找不到它

我是否可以/应该进行一些转换以使上述查询正常工作

谢谢。

使用ODM-like将减轻您的痛苦。将其添加到您的文件:

然后运行bundle安装。确保您浏览了安装指南,添加了所有必要的配置。 然后在模型/课程中加入以下内容,比如:

class Product
  include Mongoid::Document
  ...
end

您可以立即查询诸如Product.findid之类的记录。

您尝试过查看吗?您好,我一直在查看,老实说,我不确定我的测试路线是否正确。我去了一家不太理想的活动商店。不过,我已经开始在测试中使用MongoID。谢谢你的头up@Tom我刚刚创建了配置为使用MongoDB的示例资源和一些规范。
class Product
  include Mongoid::Document
  ...
end