Start a new topic
Answered

User Discovery - UserQuery for "contains"

Hi, I am having trouble with one of my user queries.  I have an array of user emails, and I want to do a UserQuery for any user that has one of those emails.  The sample code for UserQuery shows how to match an individual email:

  

    let userQuery = UserQuery {
        $0.email = "james.bond@mi6.com"
    }
    user.lookup(userQuery) { users, error in
    } 

 Is there a way to do this with something like "$0.email IN <myArrayOfEmails>?


I would rather not have to query each individually if possible.

I am using the Kinvey SDK v 3.3.9, and trying to accomplish this in Swift 3.


Thanks


Best Answer
Nick,

One way to achieve it would be to write a custom endpoint that takes in list of emails to search for:

Below is a sample custom endpoint that can help you start:


 

// you would need to read the list of emails from your request body and then use it.

function onRequest(request, response, modules) {
  var collectionAccess = modules.collectionAccess;
var logger = modules.logger;
var userColl = collectionAccess.collection('user');
var emails = ['abc@gmail.com','def@gmail.com'];
var queryUser = {"email": {"$in": emails}};
userColl.find(queryUser, function (err,users){

logger.info(users);
response.body = users;
response.complete(200);


});
}

 


I will also confirm with engineering if user discovery supports searching with multiple user email addresses.


Thanks,
Pranav
Kinvey
MLIBZ-1769

 


The workaround is good enough for me, thanks.

Nick,

The feedback from engineering is that User Discovery using multiple email addresses is not supported at this time. Are you fine with using the suggested workaround?

Thanks,
Pranav
Kinvey

 

Nick,

Glad to hear that!

Thanks,
Pranav
Kinvey

 

Thanks, the custom endpoint worked as a workaround.

Answer
Nick,

One way to achieve it would be to write a custom endpoint that takes in list of emails to search for:

Below is a sample custom endpoint that can help you start:


 

// you would need to read the list of emails from your request body and then use it.

function onRequest(request, response, modules) {
  var collectionAccess = modules.collectionAccess;
var logger = modules.logger;
var userColl = collectionAccess.collection('user');
var emails = ['abc@gmail.com','def@gmail.com'];
var queryUser = {"email": {"$in": emails}};
userColl.find(queryUser, function (err,users){

logger.info(users);
response.body = users;
response.complete(200);


});
}

 


I will also confirm with engineering if user discovery supports searching with multiple user email addresses.


Thanks,
Pranav
Kinvey
MLIBZ-1769

 

Hi, can anyone help with this issue?

I get a single user, that matches the email set inside the UserQuery brackets.  I want to be able to get a list of users, based on a list of input emails.  How would I go about doing that in a single query?

Nick,


If you try the sample code in your case, what results do you get?


let userQuery = UserQuery {
        $0.email = "james.bond@mi6.com"
    }
    user.lookup(userQuery) { users, error in
    } 


Thanks,

Pranav

Kinvey

To add a bit to this, the query I first tried was this:

let userQuery = UserQuery(format: "email IN %@", myArrayOfEmails)


This is how I would have done the query for any other Entity, but the UserQuery throws an error that it does not support this type of function.

Login or Signup to post a comment