Tom
Tom

Reputation: 1105

Ruby Mongo DB multiple records of same value

I'm new to MongoDB and databases in general. I'm using Ruby and I would like to query against a specific UUID in the database.

The ID is stored as _id and the value is '101b437a-be16-44f6-b0b0-0201cdee6510'

I have the following that usually queries my database:

  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)

end

When I look within the _id field, I'm assuming it's stored as some sort of binary key and thus isn't found using my search.

Is there some conversion I could/should do to make the query above work?

Thank you.

Upvotes: 0

Views: 163

Answers (1)

dskecse
dskecse

Reputation: 457

Using an ODM like Mongoid will ease your pain. Add it to your Gemfile:

gem 'mongoid'

and run bundle install. Make sure you skimmed through the installation guide to add all the necessary configs. Then include the following line to your model/class, say:

class Product
  include Mongoid::Document
  ...
end

You'll be able to query the records like Product.find(id) right after.

Upvotes: 1

Related Questions