检查mongo数据库中的数据

时间:2012-09-17 03:31:47

标签: mongodb

我正在使用本教程,该教程使用Sinatra,backbone.js和mongodb作为数据库。这是我第一次使用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 / Sep / 2012 08:21:58]“GET / api / todos HTTP / 1.1”200 430 0.0033

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

我开始使用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 个答案:

答案 0 :(得分: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()没有打印任何内容,那么该集合中没有数据(或者您输入了错误的名称,或使用了错误的数据库)。

相关问题