views:

32

answers:

1

Since Mongoid.master.collection() returns a collection even if the collection doesn't exist, we can use

coll = Mongoid.master.collection('analyticsCachedResult')
if coll.count == 0
  # [...]
end

to test if it is an empty collection. Another method is to loop through

Mongoid.master.collections.each do |c|
  return c if c.name == 'analyticsCachedResult'
end
return nil

but is there a simpler way to detect whether it exists?

+2  A: 

Not sure how to do it through Mongoid, but in general you can query the system.namespaces collection for {name : "dbname.analyticsCachedResult"}.

kristina
ah, thanks for the hint. This line works too: `Mongoid.master.collections.map {|c| c.name}.include? 'analyticsCachedResult'` although if Mongoid has such a call it may be better.
動靜能量