【问题标题】:I want to access the list of all emails of users present in the users collection from the accounts-ui meteor package我想从 accounts-ui 流星包访问用户集合中存在的所有用户电子邮件的列表
【发布时间】:2016-04-02 08:52:39
【问题描述】:

var user = Meteor.users.find().fetch; 这会按预期返回给我,但我应该如何只检索 users 集合中存在的所有用户的 emails.address 字段?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    这是一种可能的解决方案:

    var emails = _.chain(Meteor.users.find().fetch())
      .pluck('emails')
      .flatten()
      .pluck('address')
      .value();
    

    这将为您提供一个简单的电子邮件列表,但是请注意,在流星中,用户可以拥有多个电子邮件地址(如果您的 UI 支持)。在上面的例子中,它们都将被返回。

    【讨论】:

      【解决方案2】:

      一个简单的答案是:

      db.users.find({},{ "emails.address" : 1});
      

      这会产生这样的结果:

      { "_id" : "caGts2niaDyS6TbYJ", "emails" : [ { "address" : "asoldfj@bigglie.com" } ] }
      { "_id" : "g5oPp4ReDCwuuW8yn", "emails" : [ { "address" : "blogsalfd@sag.com" } ] }
      { "_id" : "ge9YyFehBMN7q3xQ9", "emails" : [ { "address" : "hag@toldyou.com" } ] }
      

      如果你不想要 _id:

      db.users.find({},{ "emails.address" : 1, "_id": 0});
      

      产生:

      { "emails" : [ { "address" : "asoldfj@bigglie.com" } ] }
      { "emails" : [ { "address" : "blogsalfd@sag.com" } ] }
      { "emails" : [ { "address" : "hag@toldyou.com" } ] }
      

      David Weldon 正确地指出 emails 地址字段实际上是一个数组,因此您可以在该字段中保存多个电子邮件。

      但是,如果您知道它只包含一封电子邮件,您可以使用它并搜索它,而不必担心数组。例如以下将产生一条记录:

      Meteor.users.findOne({ "emails.address" : 'asoldfj@bigglie.com' });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 2019-02-07
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多