Reputation: 207
Is there a way to dynamically send a list of string to a $in
clause for MongoDB?
I tried something like the code below, but it doesn't work and I haven't been able to find more info other than an example that has the values hardcoded.
The restrictedUsers
variable can be populated with any number of users, so I can't hardcode this.
Example:
var restrictedUsers = "bob, joe, jimmy, roger, greg";
userNames = {"usernames": { $in: [restrictedUsers]}};
collection.find(userNames);
Upvotes: 1
Views: 87
Reputation: 151112
What is wrong with using an array:
var restrictedUsers = [ "bob", "joe", "jimmy", "roger", "greg"];
userNames = {"usernames": { $in: restrictedUsers }};
collection.find(userNames);
Even if for some reason you have a string that is seperated by comma's, splitting that up into an array is fairly trivial:
var userList = "bob, joe, jimmy, roger, greg";
var restrictedUsers = userList.split(',')
Upvotes: 4