Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor.js : find users by email

Tags:

meteor

In my meteor.js app, I'm trying to write a simple admin page which can find a user by his/her email address.

I can see that in the Meteor.users collection there is an 'emails' array, which has objects like so

{ address : '[email protected]',
  verified : false
}

Normally in Mongodb I can search inside this 'emails' array like so :

Meteor.users.find({ emails.address : '[email protected]' });

But this query is throwing an error :

While building the application:
client/admin.js:224:41: Unexpected token .

Aka Meteor doesn't like the nested query...

Any ideas on how to query the Meteor.users collection by email address ?

like image 345
Petrov Avatar asked Oct 30 '13 22:10

Petrov


3 Answers

You can also use what you had, just put it in quotes:

Meteor.users.find({ "emails.address" : '[email protected]' });
like image 191
Tarang Avatar answered Oct 04 '22 23:10

Tarang


If on the server, Meteor has a special function for this : Accounts.findUserByEmail(email).

I believe this is the recommended way.

like image 20
Alexandre Bourlier Avatar answered Oct 04 '22 23:10

Alexandre Bourlier


Emails holds an array of emails. Each email has an address.

Try { emails: { $elemMatch: { address: "[email protected]" } } }.

Information on $elemMatch is here.

Information on emails as an array is here.

like image 18
user728291 Avatar answered Oct 04 '22 22:10

user728291