when i run these
Log.find(function(err, logs) {
if (err) {
return handleError(res, err);
}
console.log(logs);
});
i got the below documents
var logs = [{
mobilenumber : '1',
ref : 3,
points : 1000,
ctype : 'mycredit',
entry : 'sdfsdf',
entry : 0
},{
mobilenumber : '1',
ref : 6,
points : 2000,
ctype : 'mycredit',
entry : 'sdfsdf',
entry : 0
},{
mobilenumber : '2',
ref : 7,
points : 2600,
ctype : 'mycredit',
entry : 'sdfsdf',
entry : 0
},{
mobilenumber : '2',
ref : 15,
points : -1500,
ctype : 'mycredit',
entry : 'sdfsdf',
entry : 0
},{
mobilenumber : '10',
ref : 15,
points : 800,
ctype : 'mycredit',
entry : 'sdfsdf',
entry : 0
},{
mobilenumber : '11',
ref : 15,
points : 110,
ctype : 'mycredit',
entry : 'sdfsdf',
entry : 0
}];
var summary = [];
var positive = 0, negative = 0, total = 0, count=0;
for(var i = 0; i<logs.length; i++){
count = 0;
positive = 0;
negative = 0;
total = 0;
for(var j = i; j<logs.length; j++){
if(logs[i].mobilenumber === logs[j].mobilenumber){
if(logs[j].points < 0){
negative += logs[j].points;
}
else if(logs[j].points >= 0){
positive += logs[j].points;
}
total += logs[j].points;
count++;
}
}
i+=count-1;
var obj = {
mobilenumber : logs[i].mobilenumber,
positivepoint : positive,
negativepoint : negative,
balancepoints : total
}
summary.push(obj);
}
console.log("summarylist = ", summary);
var giftList = [];
for(var i = 0; i<summary.length; i++){
var mod = Math.floor(summary[i].balancepoints / 1000);
for(var j = 0; j<mod; j++){
var obj = {
mobilenumber : summary[i].mobilenumber,
gifed_id : 1,
ref :'gifted',
gifed_point : 1000,
active : true
}
summary[i].balancepoints -= 1000;
summary[i].negativepoint -= 1000;
giftList.push(obj);
}
}
console.log("giftlist = ", giftList);
when ever user reaches 1000 point i created new giftcard and store it on gift collection .
Every time i am calculating the user 'positivepoint' ,'negativepoint' and 'balancepoints' of all user and everything is working fine
if you run the above code you will get two object lists 'summarylist' and 'giftlist'
Now i want update summarylist data into summary collection
Summary.find(function(err, Summary) {
if (err) {
return handleError(res, err);
}
console.log(Summary);
});
i got below documents
summary collection already has three documents
{
"_id": {
"$oid": "59118950aae3ab641f2d01b6"
},
"mobilenumber": "1",
"positivepoint": 1000,
"negativepoint": 0,
"balancepoints": 1000,
},
{
"_id": {
"$oid": "950aae591183ab641fb62d01"
},
"mobilenumber": "10",
"positivepoint": 100,
"negativepoint": -10,
"balancepoints": 90,
},
{
"_id": {
"$oid": "183950aae591ab62d01641fb"
},
"mobilenumber": "14",
"positivepoint": 1000,
"negativepoint": 0,
"balancepoints": 1000,
}
now i want to update mobilenumber 1 and 10 documents and
i want to insert mobilenumber 2 and 11
how can i query and get the below result expected summary collection
{
"mobilenumber": "1",
"positivepoint": 4000,
"negativepoint": -3000,
"balancepoints": 1000,
},
{
"mobilenumber": "2",
"positivepoint": 2600,
"negativepoint": -2500,
"balancepoints": 100,
},
{
"mobilenumber": "10",
"positivepoint": 900,
"negativepoint": -10,
"balancepoints": 890,
},
{
"mobilenumber": "11",
"positivepoint": 110,
"negativepoint": 0,
"balancepoints": 110,
},
{
"mobilenumber": "14",
"positivepoint": 1000,
"negativepoint": 0,
"balancepoints": 1000,
}
via its me
No comments:
Post a Comment