ruby - Mongoid 'or' and 'in' - querying multiple arrays -
I have a document in Mongo with two array areas:
field: ap_uussernem , Type: array field: email_resource, type: array I want to create a function that takes an array of user names and an array of email addresses to search the archive. The kicker is that I want it to return those documents which any def find_people (usernames_to_search, emails_to_search) ... given values in arrays. A document is given with field values:
ap_jolation name = ['test1', 'test2', 'test3'] email_andres = ['Test1 @ test com', 'test2@test.com'] I think the function is to find it when those values are searched, array parameters I through. This document should be returned in the following cases:
search_people, ['test1@test.com'] search_folk ['test3'], ['test1@test.com' ] Search_folk ['old name'], ['test1@test.com'] The last one is bothering me.
So far I have tried
. Or (: app_usernames.in usernames_to_search ,: email_addresses.in emails_to_search) But there is no benefit.
or means to be done with a list of personal situations It can turn it on: like a MongoDB query x.or (condition1, condition2) :
$ Or: [condition1, condition2] when you say:
. Or (: app_usernames.in => UserName_ to_Search ,: email_addresses.in => Email_auto_security) How many arguments are you passing through or Are you The answer is one you are actually saying: . Or ({: app_usernames.in = & gt; user name_to_search ,:mail_addresses.in = & gt; emails_to_search}) You have to add yourself the suspicions, Ruby argues Did not break into a hash:
. Or ({: app_usernames.in = & gt; user name_to_search}, {: email_addresses.in = & gt; emails_to_search}) or something like this:
args = [] args.push (: app_usernames.in => user name_to_search) if (edit user name_orse?) Args.push (: email_addresses.in => email_and_search) if (email_to_search.present ?) Query = query Or (* args) if (args.present?)
Comments
Post a Comment