2016-04-22 57 views
0

這裏是我的文檔:蒙戈搜索嵌套數組鍵的值

"_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" : "[email protected]", 
     "verified" : false 
    } 
], 
"profile" : { 
    "first_name" : "Adam", 
    "last_name" : "Moisa" 
} 

我想在電子郵件[I]地址

搜索電子郵件這就是我已經試過(我。使用流星; Meteor.users.find({})fetch()方法返回數據庫中的所有用戶作爲對象格式化像上面):

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

我想,要返回上述目的爲「亞當」是一個電子郵件在電子郵件[0]地址

謝謝!

回答

0

無需指定索引。

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

0

指數並不需要指定,所以你可以這樣做:

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

但是,如果你想使用一個特定的指數,這樣做:

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

想通了:

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

您可以只做emails.address,它會將任何地址鍵與來自電子郵件中任何數組的值相匹配。

(從searchSource使用正則表達式,使其與任何匹配的工作:

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