askhat
askhat

Reputation: 59

Searching for a document in MongoDB by value of it's array - Mongoid/Sinatra

I got document in Mongo

{
"_id": ObjectId("53638084e1054e706f000001"),
"name": "Vasya",
"order": [
  "burger",
  "nuggets",
  "mtdew"
  ],
}

Now I need to find it

get '/order/:order' do
  @clients = Client.where(order: [':order'])
  haml :index
end

But it returns nothing. Please help me with this action.

Upvotes: 1

Views: 95

Answers (1)

Sebastian
Sebastian

Reputation: 17443

If you just want to find documents with a specific element in the order array:

db.orders.find({"order": "nuggets"})

In Ruby syntax it should be (untested):

get '/order/:order' do
  @clients = Client.where(order: params[:order])
  haml :index
end

Upvotes: 1

Related Questions