1
votes

I am using the mongoid any_of query to find a lead with an appointment status_selected of "option1" OR a lead source_selected of "opt4". I have a lead document that fits this criteria:

> db.leads.find()
{ "_id" : ObjectId("556e4f576d61638512060000"), "appointment status_selected" : "option1", "lead source_selected" : "opt2", "updated_at" : ISODate("2015-06-03T00:50:31.029Z"), "created_at" : ISODate("2015-06-03T00:50:31.029Z") }

However, when I query using the mongoid query helpers, I get no results:

Lead.where({"appointment status_selected" => "option1"}).any_of({"lead source_selected" => "opt4" }).first
# => nil

This is what moped shows:

MOPED: 127.0.0.1:27017 QUERY database=core_development collection=leads selector={"appointment status_selected"=>"option1", "$or"=>[{"lead source_selected"=>"opt4"}]} flags=[] limit=-1 skip=0 batch_size=nil fields={:_id=>1} runtime: 13.7830ms MOPED: 127.0.0.1:27017 QUERY database=core_development collection=leads selector={"appointment status_selected"=>"option1", "$or"=>[{"lead source_selected"=>"opt4"}]} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 4.1470ms

Why is Mongoid not returning that record in MongoDB?

2

2 Answers

0
votes

I would try this:

Lead.any_of({"appointment status_selected" => "option1"}, {"lead source_selected" => "opt4"}).first

While I'm not familiar with Mongoid (or more precisely, Origin), from what I can read of the Origin docs on selection, at least one of the $or criteria must be true for the row to return. Since you're only providing one criterion to your #any_of call, it may as well be another #where call.

0
votes

You've asked Mongo to return all docs where "appointment status_selected": "option1" is true and also at least one of the statements in the arguments of any_of. Since there is only one argument, this one must be true in order for a document to be returned. However, your document has lead source_selected: 'opt2', so it doesn't match.