I have a MongoDB that spits record onto a webpage
require 'mongo'
require 'json'
connection = Mongo::Connection.new
db = connection.db("salemDB")
db = Mongo::Connection.new.db("salemDB")
newsCollection = db["news"]
require 'sinatra'
set:port, 2222
get '/' do
redirect 'index.html'
end
get "/checkMail" do
newsCollection.find_one({}, {}).to_a.to_json
end
get "/:id" do
newsCollection.find("_id" => params[:id]).to_a.to_json
end
/checkmail outputs this
(formatted for reading pleasure)
[
[
"_id",
{
"$oid":"50880c8564a15e2631000001"
}
],
[
"date",
"2012-10-24T17:42:54+02:00"
],
[
"subject",
"This is a piece of news"
]
]
/50880c8564a15e2631000001 outputs this
[]
Why won’t it give my object back?
That’s because the id actually is not a string or Integer it’s an
BSON::ObjectId, so you have to query with one of those.This should work