ruby on rails - Mongoid Scope Check If Array Field Contains Value -


The solution to this question can be very easy, but I'm waving around with no answer.

I am using quietly in my Rail 4.1.4 app. I have a model in which an array Field which will house string values.

I want a Mongoyeds to get examples in the scope of the model , where it is specific in the array field String value, which is given as a parameter for the scope. Suppose we have this model:

  class Monogel in some models :: documents are included in the moguid :: timestamp field: some_array, type: array, default: array.non scope: some_scope, - & gt;  field  does not work, because obviously,  MongoDB   Em> $ elemMatch  needs to get  criteria  as  value . But, how to say  benchmark  that the element should be equal to the given value. ???  

How to write a very simple scope but a clue? ??

Regards !!! thank you in advanced.

You are working a lot more if a field contains an array, you can search it As if it was not an array. For example, if you have a document:

  {some_array: ['ou', 'is', 'pancakes', 'home?' ]}   

and you do a query like this:

  where (: some_array => 'pancakes')   

You will find that document. You do not need or are not anything complicated here; You can pretend that the array has a single value for simple questions like yours:

  scope: some_scope, - & gt; (Value) {where (: some_array = & gt; value)}   

You just need to join $ elem match If you want to apply several conditions in the element, then $ elemMatch document:

  result: {$ elemMatch: {$ gte: 80, $ lt: 85}} // ^^^^^^^ array ^^^^^^^^ ^^^^^^^^^^^^^ many terms   

do not mind , Existing mangaodi docs are not quite clear on this stuff (or at least I can not get a clear explanation. )

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -