Ruby MongoDB返回空数组
我有一个MongoDB,它会把记录吐到网页上Ruby MongoDB返回空数组,ruby,mongodb,sinatra,Ruby,Mongodb,Sinatra,我有一个MongoDB,它会把记录吐到网页上 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 'in
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会输出此信息
(为阅读乐趣而格式化)
/50880c8564a15e2631000001输出此
[]
为什么它不把我的对象还给我?这是因为id实际上不是字符串或整数,而是一个
BSON::ObjectId
,所以您必须使用其中一个进行查询
这应该行得通
newsCollection.find("_id" => BSON::ObjectId(params[:id])).to_a.to_json
newsCollection.find("_id" => BSON::ObjectId(params[:id])).to_a.to_json