Reputation: 604
What am I doing wrong?
db = Mongo::MongoClient.new(DB_URI)
tags = db['tags'].find()
tag_ids = tags.map { |t| t['_id'] }
puts tags.count
=> 4272
tags.each do |t|
puts t.inspect
end
=> # does not produce anything. As if it was empty.
If i'm comment this line
# tag_ids = tags.map { |t| t['_id'] }
, the each method works correctly.
using ruby 1.9.3p545
and gem mongo 1.9.2
Upvotes: 0
Views: 69
Reputation: 151122
The result of .find()
is a cursor not an array or list. Cursors with databases only ever work one way and once only. You already pulled all the results out, so yes it is now empty when you go to use it again.
The .count()
method returns meta-data from the cursor that holds the number returned. So usage of that is okay
You can use the .rewind()
method. But all this really does is execute the query again. So that may or may not be what you want.
If you need to use the list several times, then pull all the results to their own array and do you different iterations that way. Or figure a way to do what you want in "one pass".
Upvotes: 2