Mongoose查询,使用_id数组来查找匹配的_id' s

时间:2018-06-12 19:26:07

标签: javascript database mongodb mongoose filter

我创建了一个mongoose查询,在数据库中搜索除已经检查过的项目之外的所有匹配项目。因此,当我找到某些内容时,我会将其存储在附加到已检查项目的数组中。然后我再次检查以避免重新发送我已发送的电子邮件。

但是,$ $不会删除数组中的任何内容。

newEventCreation = async (req, res) => {
    query = {};
    let cont;
    console.log('line 136', req.body);

    if (req.body.data.UserRole) {
      const student = req.body.UserRole.find(e => e === 'STUDENT');
    if (student) query.UserRole = 'STUDENT';
    };
    if (req.body.City) query.City = { $in: req.body.City };
    if (req.body.Interests) query.Interests = { $in: req.body.Interests };
    if (req.body.EmailedUsers) query.EmailedUsers = { $not: { $in: req.body.EmailedUsers } };
    // EmailedUsers is a list of mongo _id's
    await console.log('line 149', query);

    await User.find(query, '_id Email Language Unsubscribe')
    .then(user => {
      console.log('line 153', user);
      if (user && user.length > 0) {
        user.forEach(e => {
          Email.sendEventEmail(e.Email);
          Event
            .findById(req.body._id)
            // .update({ $push: { EmailedUsers: user._id } })
            .then(event => {
              console.log('line 161', event);
              event.EmailedUsers.push(req.body._id);
              event.save(err => err ? console.log(err) : console.log('success'));
            })
            .catch(err => console.log(err));
        });
      } else res.json({ message: 'Failure'});
    })
    .catch(err => console.log(err));
  };

0 个答案:

没有答案