0

Here's my document:

"_id" : "dAWcFHJzDPJ2XT9Sh",
"createdAt" : ISODate("2016-04-22T18:03:47.761Z"),
"services" : {
    "password" : {
        "bcrypt" : "$2a$10$NYf53o/Uu8PvHPsGllRGA.WLbVpspNM4jk/6FtCzZLW.70.uQ2HXe"
    },
    "resume" : {
        "loginTokens" : [ 
            {
                "when" : ISODate("2016-04-22T18:03:47.771Z"),
                "hashedToken" : "dECxxuV/QyU2AU+/Zcrqc2Ftq64ZTrdHj5mN/rTGrxU="
            }
        ]
    }
},
"emails" : [ 
    {
        "address" : "Adammoisa@gmail.com",
        "verified" : false
    }
],
"profile" : {
    "first_name" : "Adam",
    "last_name" : "Moisa"
}

I want to search for an email in emails[i]address

Here's what I've tried (I'm using Meteor; Meteor.users.find({}).fetch() returns all users in database as objects formatted like above):

Meteor.users.find({"emails[0]address": "Adam"}).fetch();

I want that to return the above object as "Adam" is an email in emails[0]address

Thanks!

Adam Moisa
  • 1,373
  • 2
  • 14
  • 13

3 Answers3

0

No need to specify the index.

Meteor.users.find({"emails.address": "Adam"}).fetch();

aedm
  • 5,596
  • 2
  • 32
  • 36
0

The index doesn't need to be specified, so you can do this:

Meteor.users.find({"emails.address": "Adam"}).fetch();

However if you do want to use a specific index, do this:

Meteor.users.find({"emails[0].address": "Adam"}).fetch();
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
0

Figured it out:

Meteor.users.find({"emails.address": "Adammoisa@gmail.com"}).fetch();

You can just do emails.address and it will match any address key with your value from any array in emails.

(Used regex from searchSource to make it work with anything that matches:

function buildRegExp(searchText) {
 // this is a dumb implementation
  var parts = searchText.trim().split(/[ \-\:]+/);
  return new RegExp("(" + parts.join('|') + ")", "ig");
}
Adam Moisa
  • 1,373
  • 2
  • 14
  • 13