I've got a collection of complex documents.
{
<some fields>,
meta_info: {
company_name: String,
invalid: Boolean,
mobile_layout: String,
<more fields>
}
<lots more fields>
}
I ask Rails to find me all those documents where meta_info.invalid
is true/false/nil using
finished_acts.where('meta_info.invalid' => true/false/nil)
Now there is ONE document where the field does not exist. I ask...
finished_acts.find('meta_info.invalid' => {'$exists' => false})
=> nil
which is simply untrue (plus it also yields nil if I ask {'$exists' => true}
), and so is
finished_acts.where('meta_info.invalid' => {'$exists' => false}).count
=> 0
How can I find this document? I've spent days with a collection of statistical data which was always off by one count when compared to the info given me by the database itself, and this nonexistent field was the reason.
I am using mongoDB V3.4.17 and Mongoid 6.1.0.
EDIT: I've since learned that I used the .find
command incorrectly, it is only intended to be used for the _id
field and does not accept JSON/Hashes.
My problem obviously shows a bug in the implementation of the Active Record adaptation of Mongoid, and I am slowly converting my code to always use aggregations. When doing so, I get the correct number of documents. Of course, the structure returned by aggregations is more complex to handle since it is only hashes and arrays, but if that's the trade-off for getting correct results I am happy with it.