2017-01-07 17 views
0

私はモンゴ/マングースに新たなんだと私は次のことを実行するためにどのように問題を抱えています:削除/追加要素に応じて

私はそれがナイトクラブ/バーを見つけるために、YelpのAPIを利用して簡単なアプリを作成していますユーザーが検索できる地域の周りに

ユーザーにはクラブのリストが表示され、各リストには、特定のクラブに予約されている他のユーザーを追跡できるmongodbに送信されるフォームがあります。

私はclubIDはクラブのIDとゲストは、単にユーザ名を追跡します文字列の配列であるClubs

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

のスキーマを作りました。場合

1)特定のclubIDがデータベースに存在しない場合、それはguests

2でuserNameを新しいものを作成して挿入されます):

は、私は次のようなことをしたいですclubIDが存在し、userNameは(それが別のユーザーのことを意味する)guestsに存在しないclubIDが存在し場合には、guestsアレイ

3)に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"}) 
    }) 
    }) 
} 
+0

何が欲しいですか? – Codesingh

+0

基本的に1)2)と3) – Alejandro

答えて

1

これを試してみてください:

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); 
     } 
    ); 

} 

乾杯:)

1

私は理解している場合をはuserName

guestsから、私は以下の擬似コードをしていることを取り除くだけでなくguestsに存在しますあなたの質問は正しく、すべてのコードは意図どおりに動作します。ただ更新する方法を知りたいだけです特定のクラブのエントリ。これを行うには、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) { 
    ... 
}; 
関連する問題