2016-06-27 2 views
1

私は定義したパラメータに基づいて、ドキュメントの配列からオブジェクトを削除するMongoDB/NodeJSに更新クエリを書きました。これらのオブジェクトを取得した後、更新クエリによって変更されたドキュメントの数に基づいて、ドキュメント内の別の変数を増やしたいと考えています。ここでMongoDB/NodeJSを使用して、更新クエリで変更されたドキュメントの数をどのように増やすことができますか?

は私のイベントのドキュメントの1つの例です。ここで

{ 
     "_id" : ObjectId("575ed7fca7b89bb4027dded9"), 
     "dateCreated" : "6/13/2016", 
     "latitude" : "56.294786195890076", 
     "longitude" : "-43.59161567687988", 
     "instructorName" : "Test User", 
     "instructorEmail" : "[email protected]", 
     "instructorRating" : 5, 
     "eventName" : "We gon exercise", 
     "eventDescription" : "We gon exercise", 
     "spacesAvailable" : 15, 
     "streetAddress" : "123 wer", 
     "city" : "rty", 
     "state" : "NY", 
     "zip" : "12332", 
     "date" : "06/21/2016", 
     "startTime" : "12:00", 
     "endTime" : "02:10", 
     "tags" : [ 
       "Cardio", 
       "Crossfit" 
     ], 
     "price" : 5, 
     "attendies" : [ 
       { 
         "_id" : ObjectId("5759cfcdb71d80fb2d1203ef"), 
         "name" : "Buddy Loester", 
         "email" : "[email protected]", 
         "timeStamp" : 1467048318510, 
         "payed" : true 
       }, 
       { 
         "_id" : ObjectId("574f257b05086e2c7f7940ca"), 
         "name" : "Trainer Trainer", 
         "email" : "[email protected]", 
         "timeStamp" : 1467055627894, 
         "payed" : true 
       } 
     ], 
     "unpayed" : 0 
} 

は、より良い視覚化を与えるために私のコードです:

var eventCollection = req.db.get('events'); 

// get current time since epoch in milliseconds 
var milliSinceEpoch = new Date().getTime(); 

eventCollection.update(
{"attendies.payed" : {$eq : false}}, 
{ 
    $pull: 
    { 
     "attendies" : {"timeStamp": {$lt: milliSinceEpoch /*- 600000*/}} 
    }, 
    $inc: 
    { 
     spacesAvailable: numberAffected 
    } 
}, 
{ 
    multi: true 
}, function(err, numberAffected) { 

    console.log(numberAffected); 

    return res.end(); 

    } 
); 

私はにクエリ部分に「numberAffected」を指定した場合'1'、それは期待どおりに動作し、1ずつインクリメントします。ただし、影響を受ける数だけインクリメントしたいと思います。

私はこのコードがクエリで 'numberAffected'と動作しないことを知っています。コールバックで 'numberAffected'を使用すると、実際にはクエリで変更されたドキュメントの数が返されます。

私がしようとしていることをMongoDBに行う方法がありますか?

答えて

0

私は質問を書き直すことで私の問題を解決しました。それは以下の通りです:

var ObjectID = require("mongodb").ObjectID; 
    var eventCollection = req.db.get('events'); 
    var milliSinceEpoch = new Date().getTime(); 

    // find and return all the documents in the events DB where there is a user who has not payed for an event 
    // they RSVP'd for 
    eventCollection.find({"attendies.payed" : {$eq : false}}, function(err, documentsWithUnpayedUsers) { 

     // if error finding, print it and return 
     if(err) { 
      console.log(err); 
      return res.sendStatus(400, "Error cancelling"); 
     } 

     // if everyone has payed for all RSVP'd events 
     if(!documentsWithUnpayedUsers) return res.sendStatus(404, "Everyone has payed!"); 

     // loop through every document which has people who have not yet payed for RSVP'd events 
     for(var i = 0; i < documentsWithUnpayedUsers.length; i++) { 

      // for each of these documents: 
      eventCollection.update(
      {_id: ObjectID(documentsWithUnpayedUsers[i]._id)}, 
      { 
       // remove the user from the attendie list if they have not payed, 
       // and it has been 10 minutes since they RSVP'd 
       $pull: 
       { 
        "attendies" : {"timeStamp": {$lt: milliSinceEpoch - 600000}, "payed" : {$eq : false}} 
       }, 
       // then modify the number of spaces available for the event by the number of people who were 
       // removed from the attendie list 
       // then modify the amount of people who have not payed for the event yet (will now be 0) 
       $inc: 
       { 
        spacesAvailable: documentsWithUnpayedUsers[i].unpayed, 
        unpayed: -documentsWithUnpayedUsers[i].unpayed 
       } 
      }, function(err) { 
       // error checking for the update query 
       if(err){ 
        console.log(err); 
        return res.sendStatus(400, "There was an error removing an attendie fom the event: " 
        + documentsWithUnpayedUsers[i].eventName); 
       } 
       } 
     ); // end of update query 
     } // end of for loop 

     return res.end(); 

     } 
    ); // end of find() 
    }); // end of checkPayed 
関連する問題