使用meteor + react和mongoDB

时间:2016-02-26 09:27:25

标签: mongodb meteor

如何获取同一个class_id中学生的电子邮件地址,因为数据库中不同班级的学生人数超过2名?

我有这个,但它返回空数组[]

Meteor.users.find({"course_learn_list.$.class_id": {$in: [classId]}}, 
            {field: {"emails.address": 1}}
        ).fetch()

集合

{
      "_id": "LMZiLKs2MRhZiiwoS",
      "course_learn_list": [
        {
          "course_id": "M8EiKfxAAzy25WmFH",
          "class_id": "jePhNgEuXLM3ZCt98"
        },
        {
          "course_id": "5hbwrfbfxAAzy2nrg",
          "class_id": "dfbfnEuXLM3fngndn"
        }
      ],
      "emails": [
        {
          "address": "student1@gmail.com",
          "verified": false
        }
      ]
    },
    {
      "_id": "JgfdLKs2MRhZJgfNgk",
      "course_learn_list": [
        {
          "course_id": "M8EiKfxAAzy25WmFH",
          "class_id": "jePhNgEuXLM3ZCt98"
        },
        {
          "course_id": "5hbwrfbfxAAzy2nrg",
          "class_id": "dfbfnEuXLM3fngndn"
        }
      ],
      "emails": [
        {
          "address": "student2@gmail.com",
          "verified": false
        }
      ]
    }

3 个答案:

答案 0 :(得分:0)

我想你想要:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

final String username = "mymail@gmail.com";
final String password = "pass";

public SendMail() {

}


public void send(){
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,  //<- points1 here
      new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
            }
      });

    try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from-email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("to-email@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

    } catch (MessagingException e) {
            throw new RuntimeException(e);
    }
}   

这应该在每个course_learn_list数组中找到第一个实例,其中classId是你的classId。

答案 1 :(得分:0)

在这种情况下,您可能不需要使用投影来获得正确的答案。以下是仅使用选择器中的.运算符提取经过验证的电子邮件地址的示例:

const ids = ['jePhNgEuXLM3ZCt98', 'some-other-id'];
const emails =
  Meteor.users.find({ 'course_learn_list.class_id': { $in: ids } })
  .fetch()
  .map(user => _.findWhere(user.emails, { verified: true }).address);

答案 2 :(得分:0)

这对我有用!

Meteor.publish("getMyClassStudents", function(classId) {
    console.log("Publish getMyClassStudents")
    var self = this
    if (self.userId) {
        var  data = Meteor.users.find({
            "course_learn_list.class_id": classId
        }, {
            "fields": {
                "emails.address": 1
            }
        })
        return data
    }
    else {
        return self.ready()
    }
})