Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was searching a solution for the above problems for the last 2 days. Finally, found the solution at <a href="https://github.com/karmi/tire/issues/775" rel="nofollow">https://github.com/karmi/tire/issues/775</a>.</p> <p>Explanation of the problem and solution</p> <pre><code>&gt; Agent.all.collect(&amp;:name) =&gt; ["first", "second", "third", "agent smith"] &gt; Agent.all.collect(&amp;:id) =&gt; ["52204be3f94adc86b2000007", "52204be3f94adc86b2000006", "52204be3f94adc86b2000005", "52204be3f94adc86b2000004"] &gt; Agent.all.collect(&amp;:tags) =&gt; ["press pune", "press", "pune", "matrix"] $ a = Agent.search('pu') =&gt; #&lt;Tire::Results::Collection:0xbb24cf0 @response={"took"=&gt;1, "timed_out"=&gt;false, "_shards"=&gt;{"total"=&gt;5, "successful"=&gt;5, "failed"=&gt;0}, "hits"=&gt;{"total"=&gt;2, "max_score"=&gt;0.5, "hits"=&gt;[{"_index"=&gt;"agents", "_type"=&gt;"agent", "_id"=&gt;"{\"$oid\"=&gt;\"52204be3f94adc86b2000005\"}", "_score"=&gt;0.5, "_source"=&gt;{"name"=&gt;"third", "tags_array"=&gt;["pune"]}}, {"_index"=&gt;"agents", "_type"=&gt;"agent", "_id"=&gt;"{\"$oid\"=&gt;\"52204be3f94adc86b2000007\"}", "_score"=&gt;0.5, "_source"=&gt;{"name"=&gt;"first", "tags_array"=&gt;["press pune"]}}]}}, @options={:load=&gt;true, :size=&gt;10}, @time=1, @total=2, @facets=nil, @max_score=0.5, @wrapper=Tire::Results::Item&gt; 2 results present for the above search. $ a.results =&gt; Mongoid::Errors::DocumentNotFound: Problem: Document(s) not found for class Agent with id(s) {"$oid"=&gt;"52204be3f94adc86b2000005"}, {"$oid"=&gt;"52204be3f94adc86b2000007"}. Summary: When calling Agent.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): {"$oid"=&gt;"52204be3f94adc86b2000005"}, {"$oid"=&gt;"52204be3f94adc86b2000007"} ... (2 total) and the following ids were not found: {"$oid"=&gt;"52204be3f94adc86b2000005"}, {"$oid"=&gt;"52204be3f94adc86b2000007"}. Resolution: Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples. .... from /home/prasad/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-85e146637503/lib/mongoid/findable.rb:88:in `find' from /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:156:in `__find_records_by_ids' .... from /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:144:in `block in __get_results_with_load' from /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:131:in `each' from /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:131:in `__get_results_with_load' from /home/prasad/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/results/collection.rb:27:in `results' from (irb):2 # method used in tire to retrieve the objects based on the id(s). id or array of ids. def __find_records_by_ids(klass, ids) @options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load]) end </code></pre> <p><a href="https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md" rel="nofollow">https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md</a> the mongoid 4 changelog specifies that</p> <pre><code>#2497 Calling to_json no longer tampers with the return value from the driver, and proper returns{ "$oid" : object_id.to_s } instead of just the string representation previously </code></pre> <p>Tire expected an array of ids but got an hash as ids: {"$oid"=>"52204be3f94adc86b2000005"}, {"$oid"=>"52204be3f94adc86b2000007"} and tried to find the object as </p> <pre><code>Agent.find({"$oid"=&gt;"52204be3f94adc86b2000005"}, {"$oid"=&gt;"52204be3f94adc86b2000007"}) #and hence the error. </code></pre> <p>Below code works:</p> <pre><code>Agent.find("52204be3f94adc86b2000004", "52204be3f94adc86b2000007") Agent.find(["52204be3f94adc86b2000004", "52204be3f94adc86b2000007"]) Agent.all.collect(&amp;:name).as_json =&gt; ["first", "second", "third", "agent smith"] Agent.all.collect(&amp;:id).as_json =&gt; [{"$oid"=&gt;"52204be3f94adc86b2000007"}, {"$oid"=&gt;"52204be3f94adc86b2000006"}, {"$oid"=&gt;"52204be3f94adc86b2000005"}, {"$oid"=&gt;"52204be3f94adc86b2000004"}] </code></pre> <p>the temporary hack: add a initializer with below content</p> <pre><code>require "tire" module Tire class Index def get_id_from_document(document) #document is the object. ie agent object in the above case case when document.is_a?(Hash) document[:_id] || document['_id'] || document[:id] || document['id'] #if is not an hash and responds to 'id' and object_id is not equal to id "Agent.last.object_id =&gt; 96643940" when document.respond_to?(:id) &amp;&amp; document.id != document.object_id document.id.to_s # was document.id.as_json end end end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload