2016-05-05 10 views
0

Ich versuche, mehrere Benutzerbilanzen zu aktualisieren, wenn Matching ID vorhanden ist.Mongoose - Update Balance für jede ID

ich Mongoose bin mit, das ist, wie mein gespeichertes Dokument wie folgt aussieht:

{ tokens: [], 
     profile: { gender: '', location: '', website: '', picture: '' }, 
     __v: 0, 
     balance: 0, 
     userWalletID: '2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE', 
     password: '$2a$10$JRs/hyiStyhcZzHBwFnhmulKX0vTqlNMTGdxJjDdCSD0Bufe7K6wC', 
     email: '[email protected]', 
     username: '111111111111111111111', 
     createdAt: Mon May 02 2016 01:38:11 GMT+0300 (EEST), 
     updatedAt: Mon May 02 2016 01:38:17 GMT+0300 (EEST), 
     _id: 57268553ea2d6bb50c9d4069 } 
    { tokens: [], 
     profile: { gender: '', location: '', website: '', picture: '' }, 
     __v: 0, 
     balance: 0, 
     userWalletID: '2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC', 
     password: '$2a$10$cCo5gQgqcRpQW9Dl/Q.48uJEQCaOpJOLY8LVpQxoQFZ6nUqQD8lEC', 
     email: '[email protected]', 
     username: '2222222222222222222222222', 
     createdAt: Mon May 02 2016 01:38:38 GMT+0300 (EEST), 
     updatedAt: Mon May 02 2016 01:38:44 GMT+0300 (EEST), 
     _id: 5726856eea2d6bb50c9d406a } 
    { tokens: [], 
     profile: { gender: '', location: '', website: '', picture: '' }, 
     __v: 0, 
     balance: 5645645, 
     userWalletID: '2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL', 
     password: '$2a$10$tL96HqPIAi6q8XnI3z4Bc.dX88fTYQXsTt.UFWJQn4k6ZdDFOZLe2', 
     email: '[email protected]', 
     username: '33333333333333333333333', 
     createdAt: Mon May 02 2016 01:39:03 GMT+0300 (EEST), 
     updatedAt: Mon May 02 2016 01:39:09 GMT+0300 (EEST), 
     _id: 57268587ea2d6bb50c9d406b } 

Als nächstes habe ich User.find() alle userWalletID zu erhalten und einen Anruf laufen bekommen eine JSON OBJ zu erhalten, enthaltend Alle Wallet-Informationen für alle userWalletID in meiner Datenbank.

User.find({}, function(err, docs) { 
    if (err) {console.log('Could not find Wallets in DB');} 
     var allDBWallets = []; var allOBJWallets = []; 

    docs.forEach(function(address) { 
     console.log(address); 
     allDBWallets.push(address.userWalletID); 
    }); 

    for(var iteration in allDBWallets){ 
     bitgo.wallets().get({ "id": allDBWallets[iteration] }, function callback(err, wallet) { 
      if (err) { console.log(err); } 
      allOBJWallets.push(wallet); 
      jsonfile.writeFileSync(allConfirmedBalance, allOBJWallets, {spaces: 3}); 
     }); 
    } 

    }); 

Ich bekomme ein Array mit 3 Objekten zurück.

[ 
    { 
     "_id": "5726858f42929c5c066825b6cc7f9d96", 
     "id": "2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL", 
     "label": "123labeling", 
     "isActive": true, 
     "type": "safehd", 
     "freeze": {}, 
     "adminCount": 1, 
     "disableTransactionNotifications": false, 
     "private": { 
      "keychains": [ 
       { 
       "xpub": "xpub661MyMwAqRbcG8zA12U9YnJU3fSxoCq857uMcFon6ViZdYMSeXbeVUGtGkBCasmE3SGdJVRr4ui1V8TzsmM7K6eKikSi9gZQiJXoVumGTnU", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "031ec65814f7e46e274a78671f6ed2b945c386e378160b19613ce8ba555e793635", 
        "chainCode": "a046f6f6ea9fd06974b333e87a483c9c3cbdeb41c0e56c8f21a6954f9f95e387", 
        "depth": 0, 
        "index": 0, 
        "parentFingerprint": 0 
       } 
       }, 
       { 
       "xpub": "xpub6GiRC55CRvMEnS2CtwqrMNKDnpsogbX1tukjM2xWyW1PttVLSEVgDiJq7bWh6esAQWeQ9oGNR56RHqwzKLCzzneM6DGq91v8sA9DAJbUbtm", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "027f30812a8f33ffeb44515d0fe9cc99041c2369f407ecc537d492d34da48c29d2", 
        "chainCode": "4f493067187019f78a773e3e559c699b98c0e82a53cbf76cf9ba5db8103062a0", 
        "depth": 5, 
        "index": 58402, 
        "parentFingerprint": 2966462100 
       } 
       }, 
       { 
       "xpub": "xpub661MyMwAqRbcF6cBQfmTER34dwShTVu6x2h24nxhHxMX3jAHCP9DJYuLcHABvaMeaBcSMdcte8mhAMnCcBCypK5iGe6H33aUH8JWbstSKP9", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "02ec33597217f00f1ad5689b0abbfd01d850c921d4560d1333f52f9f70eea2aafd", 
        "chainCode": "37b746ccdf38587d6a943f8007ef9e3012f52c73079f53c094f446cd43f9bb4f", 
        "depth": 0, 
        "index": 0, 
        "parentFingerprint": 0 
       } 
       } 
      ] 
     }, 
     "canSendInstant": true, 
     "permissions": "admin,spend,view", 
     "admin": { 
      "users": [ 
       { 
       "user": "56f9e3b2cd8f2e5906530948fe1af2d2", 
       "permissions": "admin,spend,view" 
       } 
      ] 
     }, 
     "tags": [], 
     "approvalsRequired": 1, 
     "spendingAccount": true, 
     "pendingApprovals": [], 
     "balance": 310488966, 
     "instantBalance": 310488966, 
     "spendableConfirmedBalance": 310488966, 
     "confirmedBalance": 310488966, 
     "spendableBalance": 310488966, 
     "sent": 0, 
     "received": 310488966, 
     "unconfirmedSends": 0, 
     "unconfirmedReceives": 0 
    }, 
    { 
     "_id": "5726857642929c5c066825819a872925", 
     "id": "2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC", 
     "label": "123labeling", 
     "isActive": true, 
     "type": "safehd", 
     "freeze": {}, 
     "adminCount": 1, 
     "disableTransactionNotifications": false, 
     "private": { 
      "keychains": [ 
       { 
       "xpub": "xpub661MyMwAqRbcH2AHKRsNyEv4euymBzY8P52NzeodZBBvG8zPtpLeEJmjD8xevZRrDLCyCr4TjmavB9TuAw69bDH3TkKNPyEQ2UBVrGg8uVq", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "038c20add130bf5d79fc1debc658e8866f5c69962efc15350941d18cc3b88c025f", 
        "chainCode": "f8e61f519f8e5363b24fe19912a25c90395a9189f59517d5c888c2b2eaefe4ba", 
        "depth": 0, 
        "index": 0, 
        "parentFingerprint": 0 
       } 
       }, 
       { 
       "xpub": "xpub6GiRC55CRvMEkcMvj2xtGSMhQzAys7EMV4gvqporNz5kSkzuqt3x25cnpvYWyR24y38WYij7eLr3xSQsBo7TBHWR27b5QZTrj5sVq5MMBVa", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "037e26f5594705a05a49e63dbd7eb98e0fb56d0f26b584c8e4fcdb15efd0239d62", 
        "chainCode": "984f19d3ae866a97a1b3a0bad1abf2a9aa77dbf50465180dee0cf8a8b73089c8", 
        "depth": 5, 
        "index": 58401, 
        "parentFingerprint": 2966462100 
       } 
       }, 
       { 
       "xpub": "xpub661MyMwAqRbcGdVpPbFpvi37Ziteqj4JY3uEoV6XijMAk62Pgs2jK3bRPPVjKg4AtwbZ6xiMMHD56oHCdGQwqtWNZgwSWefw9edex9aJ9KC", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "03b323e0b6ebb0cbb845be13c5a090c4648de18c090382e899e66c389c0f6451fc", 
        "chainCode": "d1a6e364a4b44764190148e725a8c584583743d31df2d06439b70016e19799b5", 
        "depth": 0, 
        "index": 0, 
        "parentFingerprint": 0 
       } 
       } 
      ] 
     }, 
     "canSendInstant": true, 
     "permissions": "admin,spend,view", 
     "admin": { 
      "users": [ 
       { 
       "user": "56f9e3b2cd8f2e5906530948fe1af2d2", 
       "permissions": "admin,spend,view" 
       } 
      ] 
     }, 
     "tags": [], 
     "approvalsRequired": 1, 
     "spendingAccount": true, 
     "pendingApprovals": [], 
     "balance": 1003135000, 
     "instantBalance": 1003135000, 
     "spendableConfirmedBalance": 1003135000, 
     "confirmedBalance": 1003135000, 
     "spendableBalance": 1003135000, 
     "sent": 0, 
     "received": 1003135000, 
     "unconfirmedSends": 0, 
     "unconfirmedReceives": 0 
    }, 
    { 
     "_id": "5726855c0cb29c5b060918484524d6d9", 
     "id": "2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE", 
     "label": "123labeling", 
     "isActive": true, 
     "type": "safehd", 
     "freeze": {}, 
     "adminCount": 1, 
     "disableTransactionNotifications": false, 
     "private": { 
      "keychains": [ 
       { 
       "xpub": "xpub661MyMwAqRbcFcyTCTTNVmYcAmPJ2yRJanFPv9To7n1wk5cZmgUpzY16pAz9m6XriziHnk6c4Y2Hov9HJSmWPSHdmSLn9KNBLMr5Qi29HGN", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "032536e93aa4cd5feb0706d118b6ecf695d26544c00f92a2fb36ef9208ee216821", 
        "chainCode": "6c4dbaed05b8a09d2d64c3688e3fb53f12e62ef64b70ad808a700830e2063bb2", 
        "depth": 0, 
        "index": 0, 
        "parentFingerprint": 0 
       } 
       }, 
       { 
       "xpub": "xpub6GiRC55CRvMEhQVtxhHwwpmG7NVyjT1SUuyYXfafsRDW4XoYRwzPp6Af5vY83hLN8vaFCzk9FdtUqdeXoQbS7fXNJU3eFYPnU1bfkjimsT4", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "035d12105eab700c637ca9fbbea066beca88c6603f90f98c07a9e5b84713d6ff20", 
        "chainCode": "5670e6ede3379f33cc635981c7fbe9255c3f69a5e4868e5491e42bf3d5fb380a", 
        "depth": 5, 
        "index": 58400, 
        "parentFingerprint": 2966462100 
       } 
       }, 
       { 
       "xpub": "xpub661MyMwAqRbcEZBgr8PUbKfGBaQ98GFp2wGdR6vDwTAzruGenMnn3PqDo9MMmzSsF8dFWgUfUUV1Yor2ErVrohBNHj9Ug81ih7VAohfnEQu", 
       "path": "/0/0", 
       "params": { 
        "pubKey": "024f7b2f85a3e0b859c4bdb975f9953973de4806300b2148afec66fb610af380a7", 
        "chainCode": "014ce166076a6045fb5a88fa4a51052d13c5449a4367d030e0f6bc058a9d0191", 
        "depth": 0, 
        "index": 0, 
        "parentFingerprint": 0 
       } 
       } 
      ] 
     }, 
     "canSendInstant": true, 
     "permissions": "admin,spend,view", 
     "admin": { 
      "users": [ 
       { 
       "user": "56f9e3b2cd8f2e5906530948fe1af2d2", 
       "permissions": "admin,spend,view" 
       } 
      ] 
     }, 
     "tags": [], 
     "approvalsRequired": 1, 
     "spendingAccount": true, 
     "pendingApprovals": [], 
     "balance": 2205867000, 
     "instantBalance": 2205867000, 
     "spendableConfirmedBalance": 2205867000, 
     "confirmedBalance": 2205867000, 
     "spendableBalance": 2205867000, 
     "sent": 0, 
     "received": 2205867000, 
     "unconfirmedSends": 0, 
     "unconfirmedReceives": 0 
    } 
    ] 

Jedes JSON OBJ "id": "2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC", entspricht einem DB userWalletID. Ich habe versucht, etwas auf eigene Faust zu schreiben, indem sie alle userWalletID zu einem Array hinzufügen und es als Getter vorbei wie so:

jsonfile.readFile(allConfirmedBalance, function(err, obj) { 
     var getAllWallets = []; 

     obj.forEach(function(obj){ 
     getAllWallets.push(obj.id); 
     }); 

     console.log(getAllWallets); 
[ '2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE', 
    '2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL', 
    '2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC' ] 

     User.model('User').update({"playerWallet" : {"$in":getAllWallets}}, {"$set": {"balance" : spendableConfirmedBalance[i]}}, {active:false} , {multi: true} , function(err,docs) { 
     console.log(docs); 
     }); 

    }); 

aber kläglich gescheitert,

Bare daran, dass spendableConfirmedBalance [i] ist genauso ein Beispiel, da ich keine Ahnung habe, wie man für jede einzelne passende ID ihr jeweiliges Guthaben auf den neuesten Stand bringt ....

Was ist der beste Weg, um jeden mathing IDs-Saldo zu aktualisieren: 0, mit ihrem "spendableConfirmedBalance": 2205867000?

Vielen Dank für Ihre Hilfe und tolle Ratschläge!

+0

Sie können mehrere Dokumente nicht aktualisieren und unterschiedliche Werte in einer Abfrage festlegen. Sie müssen jeden Benutzer einzeln aktualisieren. – Molda

+0

Vielen Dank, dass Sie darauf hingewiesen haben. Ich werde versuchen, es in der Iteration zu speichern, bevor ich es in JSON speichere und zu dir zurückkomme. –

Antwort

0

Ich landete es in einem Anruf. Es funktioniert großartig, danke Molda, dass du auf Mongo aufmerksam gemacht hast. Kudos !!!

var listWallets = function() { 

    User.find({}, function(err, docs) { 
     if (err) {console.log('Could not find Wallets in DB');} 
     var allDBWallets = []; var allOBJWallets = []; 

     docs.forEach(function(address) { 
     // console.log(address); 
     allDBWallets.push(address.userWalletID); 
     }); 

     for(var iteration in allDBWallets){ 
      bitgo.wallets().get({ "id": allDBWallets[iteration] }, function callback(err, wallet) { 
      if (err) { console.log(err); } 
      allOBJWallets.push(wallet); 

       for (var walletIteration in allDBWallets) { 
       if(allDBWallets[walletIteration] == wallet.wallet.id){ 
        console.log(wallet.wallet.id, wallet.wallet.spendableConfirmedBalance); 

        var conditions = { userWalletID: wallet.wallet.id } 
         , update = { $set: { balance: wallet.wallet.spendableConfirmedBalance }} 
         , options = { multi: false }; 

        User.update(conditions, update, options, function callback (err, numAffected) { 
         console.log(numAffected); 
        }); 
       } 
      } 

      jsonfile.writeFileSync(allConfirmedBalance, allOBJWallets, {spaces: 3}); 
      }); 
     } 

    }); 
};