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中有沒有辦法做我想做的事情?