2012-09-17 56 views
0

我正在玩弄本教程,该教程使用Sinatra,backbone.js和mongodb作为数据库。这是我第一次使用mongo。据我了解,该应用程序使用本地存储和数据库。它具有数据库的这些路由。例如,它有这些路线检查mongo数据库中的数据

get '/api/:thing' do 
    DB.collection(params[:thing]).find.to_a.map{|t| from_bson_id(t)}.to_json 
end 

get '/api/:thing/:id' do 
    from_bson_id(DB.collection(params[:thing]).find_one(to_bson_id(params[:id]))).to_json 
end 

post '/api/:thing' do 
    oid = DB.collection(params[:thing]).insert(JSON.parse(request.body.read.to_s)) 
    "{\"_id\": \"#{oid.to_s}\"}" 
end 

开启服务器关闭之后再上,我可以在服务器上看到从数据库中获取航线数据

127.0.0.1 - - [17 /月/ 2012 08:21:58]“GET/api/todos HTTP/1.1”200 430 0.0033

我的问题是,如何从mongo shell内检查数据是否在数据库中?

我开始蒙戈外壳

./bin/mongo 

我选择的数据库'use mydb'

,然后在看文档(http://www.mongodb.org/display/DOCS/Tutorial)我试着命令如

> var cursor = db.things.find(); 
> while (cursor.hasNext()) printjson(cursor.next()); 

但他们没有返回任何东西。

回答

1

您并不需要为了读取某些数据而创建游标。

% mongo 
MongoDB shell version: 2.2.0 
connecting to: test 
> use mydb 
switched to db mydb 
> show collections 
system.indexes 
things 
> db.things.find() 
{ "_id" : ObjectId("50569a52c0ba23837b5dd811"), "name" : "TV set" } 
{ "_id" : ObjectId("50569a5bc0ba23837b5dd812"), "name" : "car" } 

如果db.things.find()不打印任何东西,然后有这个集合中没有数据(或输入错误的名称,或使用了错误的数据库)。