mongoid/mongoid — Issue #1734: Model.find provides inconsistent behaviour
Author: paulbjensen
Created: 2012-02-20 15:43:04+00:00
State: open

Hi,

We've come across an inconsistent behaviour with Mongoid regarding the use of Model.find.

You have a model (Hangout), and 2 Object Ids for documents contained in the model's collection. Say you have a scope which filters those documents based on an attribute. When applying that scope (upcoming), the first document should not be returned, but the second should.

> hangout_ids
=> [BSON::ObjectId('4f42466d96ee61c0f10000bd'), BSON::ObjectId('4f42467596ee61c0f10000d2')]
> Hangout.upcoming.find(hangout_ids)
=> [#]

Now, let's try and pass the id of the first document into that model's scoped find method:

> Hangout.upcoming.find(hangout_ids.first)
=> Mongoid::Errors::DocumentNotFound: Document not found for class Hangout with id(s) 4f42466d96ee61c0f10000bd.

If we try the same query above but with the last item in the hangout_ids array, it returns the array with the document.

What seems to be happening instead is that if a single document is found among an array of ids, then the DocumentNotFound exception will not be raised at all. We are therefore dependent on an expectation that at least one of the ids in an array matches a document in the collection. This seems like an inconsistent behaviour.