Adding/Removing Mongoose 中相应的元素

Adding/Removing elements in Mongoose accordingly

我是 Mongo/Mongoose 的新手,我无法执行以下操作:

我正在创建一个简单的应用程序,它利用 Yelp API 在用户可以搜索的区域周围查找夜总会/酒吧。

用户会看到一份俱乐部列表,每个列表都有一个表格可以发送给 mongodb,可以跟踪为该特定俱乐部保留的其他用户。

我已经制作了 Clubs

的架构
const clubSchema = new Schema({
  clubID: String,
  guests: [String]
})

其中 clubID 是俱乐部的 ID,guests 只是一个字符串数组,用于跟踪用户名。

我想做以下事情:

1) 当数据库中不存在特定的clubID时,它将创建一个新的并将userName插入guests

2) 如果 clubID 存在而 userName 不存在于 guests 中(意味着它是不同的用户),它将把 userName 推入guests数组

3) 如果 clubID 存在并且 userName 也存在于 guests 中,从 guests

中删除 userName

我有以下伪代码:

exports.UpdateGuestList = function(req, res, next){
  const clubID = req.params.clubID;
  const userName = req.params.userName
  const userEmail = req.params.userEmail;

  Club.findOne({ clubID: clubID}, function(err, existingClub){
    if (err) { return next(err) ;}
    // if clubID exist in the data base, check the following
    if (existingClub){
                     //1) if the current userName exist, remove it from guests array
      if (existingClub.guests.includes(userName)){
        console.log('Remove guest')
      } else{         //2) if the current userName doesnt exist, push it into guests aaray
        console.log('Push in gueest')
      }
      return res.send({"message": "done"})
    }

    // If clubID does not exist, create and save clubID
    const club = new Club({
      clubID: clubID,
      guests: [userName]
    })

    club.save(function(err){
      if (err) {return next(err)}
      res.send({"message": "done"})
    })
  })
}

试试这个:

if (existingClub.guests.includes(userName)){

  Club.findByIdAndUpdate(
        clubID,
        {$pull: {"guests": userName}},
        {safe: true, upsert: true, new : true},
        function(err, model) {
            console.log(err);
        }
    );
}
else
{
  console.log('Push in gueest')
  Club.findByIdAndUpdate(
        clubID,
        {$push: {"guests": userName}},
        {safe: true, upsert: true, new : true},
        function(err, model) {
            console.log(err);
        }
    ); 

}

干杯:)

如果我正确理解你的问题,你的所有代码都按预期工作,只是你想知道如何更新特定的俱乐部条目。为此,只需修改 javascript 对象,然后修改 save() 它。

exports.UpdateGuestList = function(req, res, next){
  const clubID = req.params.clubID;
  const userName = req.params.userName;
  const userEmail = req.params.userEmail;

  Club.findOne({ clubID: clubID}, function(err, club){
    if (err) {return next(err);}
    // if clubID exist in the data base, check the following
    if (club){

      if (club.guests.includes(userName)){
        //1) if the current userName exist, remove it from guests array
        const userIndex = club.guests.findIndex(function (guest) {
          return guest === userName;
        });
        club.guests.splice(userIndex, 1);
      } else{
        //2) if the current userName doesnt exist, push it into guests array
        club.guests.push(userName);
      }

    } else {
      // If clubID does not exist, create and save clubID
      club = new Club({
        clubID: clubID,
        guests: [userName]
      });
    }

    club.save(function(err){
      if (err) {return next(err);}
      res.send({"message": "done"});
    });
  });
};

您还应该考虑将此方法作为静态方法添加到架构声明中,而不是将此方法包含在单独的库中。有关详细信息,请参阅 http://mongoosejs.com/docs/guide.html。它在您的 clubSchema 文件中可能看起来像这样:

const clubSchema = new Schema({
  clubID: String,
  guests: [String]
});

clubSchema.statics.UpdateGuestList = function (clubId, userName) {
  ...
};