Skip to main content
Code Review

Return to Question

edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Mongodb making Making an efficient and healthy db call for a real-time browser game

FUNDS.aggregate().group({ //get sum of invested amounts
 _id: '$id1',
 invest_total: { $sum: '$invest_amount'}}).exec(function ( e1, d ) {
 if( !e1 && d ){
 FUNDS.find().exec(function( e2, invests ){ //find all invests
 if ( !e2 && invests ){ 
 //calculate PROFIT value to each invest according to their rates (it can be +/-)
 for (var i = 0; i < invests.length; i++) { 
 
 var invest_amount = invests[i].amountinvest_amount,
 invest_id  = invests[i].invest_id,
 PROFIT = AN_AMOUNTAN_AMOUNT_FROM_SITE * invest_amount / d[0].invest_total; // 
 //update each invest 
  FUNDS.findOneAndUpdate({ 'invest_id': invest_id }, { $inc: { 'invest_profit': profit } }, function(e3) {
 callback()
 });
 };
 }
 })
 }
})

Mongodb making efficient and healthy db call

FUNDS.({ //get sum of invested amounts
 _id: '$id1',
 invest_total: { $sum: '$invest_amount'}}).exec(function ( e1, d ) {
 if( !e1 && d ){
 FUNDS.find().exec(function( e2, invests ){ //find all invests
 if ( !e2 && invests ){ 
 //calculate PROFIT value to each invest according to their rates (it can be +/-)
 for (var i = 0; i < invests.length; i++) { 
 
 var invest_amount = invests[i].amount,
 
 PROFIT = AN_AMOUNT * invest_amount / d[0].invest_total; // 
 
 };
 }
 })
 }
})

Making an efficient and healthy db call for a real-time browser game

FUNDS.aggregate().group({ //get sum of invested amounts
 _id: '$id1',
 invest_total: { $sum: '$invest_amount'}}).exec(function ( e1, d ) {
 if( !e1 && d ){
 FUNDS.find().exec(function( e2, invests ){ //find all invests
 if ( !e2 && invests ){ 
 //calculate PROFIT value to each invest according to their rates (it can be +/-)
 for (var i = 0; i < invests.length; i++) { 
 
 var invest_amount = invests[i].invest_amount,
 invest_id  = invests[i].invest_id,
 PROFIT = AN_AMOUNT_FROM_SITE * invest_amount / d[0].invest_total; // 
 //update each invest 
  FUNDS.findOneAndUpdate({ 'invest_id': invest_id }, { $inc: { 'invest_profit': profit } }, function(e3) {
 callback()
 });
 };
 }
 })
 }
})
deleted 299 characters in body
Source Link
Lazy
  • 163
  • 9
FUNDS.aggregate().group({ //get sum of invested amounts
 _id: '$id1',
 invest_total: { $sum: '$invest_amount'}}).exec(function ( e1, d ) {
 if( !e1 && d ){
 FUNDS.find().exec(function( e2, invests ){ //find all invests
 if ( !e2 && invests ){ 
 //calculate PROFIT value to each invest according to their rates (it can be +/-)
 for (var i = 0; i < invests.length; i++) { 
 
 var invest_amount = invests[i].invest_amountamount,
 invest_id  = invests[i].invest_id,
 PROFIT = AN_AMOUNT_FROM_SITEAN_AMOUNT * invest_amount / d[0].invest_total; // 
 //update each invest 
  FUNDS.findOneAndUpdate({ 'invest_id': invest_id }, { $inc: { 'invest_profit': profit } }, function(e3) {
 callback()
 });
 };
 }
 })
 }
})
FUNDS.aggregate().group({ //get sum of invested amounts
 _id: '$id1',
 invest_total: { $sum: '$invest_amount'}}).exec(function ( e1, d ) {
 if( !e1 && d ){
 FUNDS.find().exec(function( e2, invests ){ //find all invests
 if ( !e2 && invests ){ 
 //calculate PROFIT value to each invest according to their rates (it can be +/-)
 for (var i = 0; i < invests.length; i++) { 
 
 var invest_amount = invests[i].invest_amount,
 invest_id  = invests[i].invest_id,
 PROFIT = AN_AMOUNT_FROM_SITE * invest_amount / d[0].invest_total; // 
 //update each invest 
  FUNDS.findOneAndUpdate({ 'invest_id': invest_id }, { $inc: { 'invest_profit': profit } }, function(e3) {
 callback()
 });
 };
 }
 })
 }
})
FUNDS.({ //get sum of invested amounts
 _id: '$id1',
 invest_total: { $sum: '$invest_amount'}}).exec(function ( e1, d ) {
 if( !e1 && d ){
 FUNDS.find().exec(function( e2, invests ){ //find all invests
 if ( !e2 && invests ){ 
 //calculate PROFIT value to each invest according to their rates (it can be +/-)
 for (var i = 0; i < invests.length; i++) { 
 
 var invest_amount = invests[i].amount,
 
 PROFIT = AN_AMOUNT * invest_amount / d[0].invest_total; // 
 
 };
 }
 })
 }
})
Notice removed Canonical answer required by Community Bot
Bounty Ended with code_monk's answer chosen by Community Bot
added 80 characters in body
Source Link
Lazy
  • 163
  • 9

When this process finish, I'm saving a few unimportant data in callback().

When FUNDS collection has a few hundred document, this process will be very slow. Are there any ways I can reduce this process with Mongo to make it more efficient?

When FUNDS collection has a few hundred document, this process will be very slow. Are there any ways I can reduce this process with Mongo to make it more efficient?

When this process finish, I'm saving a few unimportant data in callback().

When FUNDS collection has a few hundred document, this process will be very slow. Are there any ways I can reduce this process with Mongo to make it more efficient?

Tweeted twitter.com/#!/StackCodeReview/status/524688852796338177
Notice added Canonical answer required by Lazy
Bounty Started worth 100 reputation by Lazy
deleted 6 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Lazy
  • 163
  • 9
Loading
default

AltStyle によって変換されたページ (->オリジナル) /