Node.js 使用MongoDB/NodeJS,如何增加更新查询中修改的文档数量?
我在MongoDB/NodeJS中编写了一个更新查询,根据我定义的参数从文档数组中删除对象。拉取这些对象后,我想根据更新查询修改的文档数量,增加文档中的另一个变量 以下是我的活动文档之一的示例:Node.js 使用MongoDB/NodeJS,如何增加更新查询中修改的文档数量?,node.js,mongodb,mongodb-query,updates,increment,Node.js,Mongodb,Mongodb Query,Updates,Increment,我在MongoDB/NodeJS中编写了一个更新查询,根据我定义的参数从文档数组中删除对象。拉取这些对象后,我想根据更新查询修改的文档数量,增加文档中的另一个变量 以下是我的活动文档之一的示例: { "_id" : ObjectId("575ed7fca7b89bb4027dded9"), "dateCreated" : "6/13/2016", "latitude" : "56.294786195890076", "longitu
{
"_id" : ObjectId("575ed7fca7b89bb4027dded9"),
"dateCreated" : "6/13/2016",
"latitude" : "56.294786195890076",
"longitude" : "-43.59161567687988",
"instructorName" : "Test User",
"instructorEmail" : "test@user.com",
"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" : "Bud18@gmail.com",
"timeStamp" : 1467048318510,
"payed" : true
},
{
"_id" : ObjectId("574f257b05086e2c7f7940ca"),
"name" : "Trainer Trainer",
"email" : "trainer@user.com",
"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();
}
);
如果我在查询部分中将'numberraffected'指定为'1',那么它将按预期工作并递增1。不过,我想增加受影响的人数
我知道这段代码不能与查询中的'numberraffected'一起使用。在回调中使用'numberraffected'实际上返回了我的查询修改的文档数
MongoDB中是否存在一种方法来完成我试图完成的任务?我通过重写查询解决了我的问题。详情如下:
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
var ObjectID=require(“mongodb”).ObjectID;
var eventCollection=req.db.get('events');
var milliSinceEpoch=new Date().getTime();
//查找并返回事件数据库中的所有文档,其中有一个用户尚未为事件付款
//他们拒绝了
eventCollection.find({“attendies.payed”){$eq:false},函数(err,documentsWithUnpayedUsers){
//如果发现错误,请打印并返回
如果(错误){
控制台日志(err);
返回res.sendStatus(400,“错误取消”);
}
//如果每个人都为所有RSVP的活动付费
如果(!documentsWithUnpayedUsers)返回res.sendStatus(404,“每个人都已付款!”);
//循环浏览每个文档,其中包含尚未为RSVP事件付款的人员
对于(var i=0;i
我通过重写查询解决了问题。详情如下:
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
var ObjectID=require(“mongodb”).ObjectID;
var eventCollection=req.db.get('events');
var milliSinceEpoch=new Date().getTime();
//查找并返回事件数据库中的所有文档,其中有一个用户尚未为事件付款
//他们拒绝了
eventCollection.find({“attendies.payed”){$eq:false},函数(err,documentsWithUnpayedUsers){
//如果发现错误,请打印并返回
如果(错误){
控制台日志(err);
返回res.sendStatus(400,“错误取消”);
}
//如果每个人都为所有RSVP的活动付费
如果(!documentsWithUnpayedUsers)返回res.sendStatus(404,“每个人都已付款!”);
//循环浏览每个文档,其中包含尚未为RSVP事件付款的人员
对于(var i=0;i