【问题标题】:checking mongo database for data检查 mongo 数据库中的数据
【发布时间】:2012-09-09 06:58:42
【问题描述】:

我正在玩这个使用 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());

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

【问题讨论】:

    标签: mongodb


    【解决方案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() 没有打印任何内容,则说明该集合中没有数据(或者您输入了错误的名称,或者使用了错误的数据库)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-16
      • 2021-07-07
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多