BrainLikeADullPencil
BrainLikeADullPencil

Reputation: 11663

checking mongo database for data

I'm playing around with this tutorial that uses Sinatra, backbone.js, and mongodb for the database. It's my first time using mongo. As far as I understand it the app uses both local storage and a database. it has these routes for the database. For example, it has these routes

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

After turning the server off and then on, I could see in the server getting data from the database routes

127.0.0.1 - - [17/Sep/2012 08:21:58] "GET /api/todos HTTP/1.1" 200 430 0.0033

My question is, how can I check from within the mongo shell whether the data's in the database?

I started the mongo shell

  ./bin/mongo  

I selected the database 'use mydb'

and then looking at the docs (http://www.mongodb.org/display/DOCS/Tutorial) I tried commands such as

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

but they didn't return anything.

Upvotes: 0

Views: 291

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230366

You don't really have to create a cursor just to read some data.

% 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" }

If db.things.find() doesn't print anything, then there's no data in that collection (or you mistyped its name, or are using the wrong database).

Upvotes: 1

Related Questions