2016-06-27 37 views
1

我在MongoDB/NodeJS中寫了一個更新查詢,它根據我定義的參數從文檔數組中刪除對象。在我拉出這些對象後,我想根據更新查詢修改了多少文檔來增加文檔中的另一個變量。使用MongoDB/NodeJS,如何增加更新查詢中修改的文檔數量?

這裏是我的事件文件的一個示例:

{ 
     "_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 
相關問題