2016-05-05 6 views
0

マッチングIDが存在する場合、複数のユーザー残高を更新しようとしています。マングース - 各IDの残高を更新

私はマングースを使用していますが、これは私の保存された文書がどのように見えるかです:

{ 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 } 

次に、私はすべてのuserWalletIDを取得し、含むJSON OBJを取得するために呼び出す取得を実行するために)(User.findを使用しました自分のDB内のすべてのuserWalletIDのすべてのウォレット情報。

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}); 
     }); 
    } 

    }); 

3つのオブジェクトを持つ配列が返されます。

[ 
    { 
     "_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 
    } 
    ] 

各JSON OBJ "id": "2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC"は、1つのDBのuserWalletIDと一致します。 spendableConfirmedBalance [i]は同様であることを念頭に、

ベアを

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); 
     }); 

    }); 

しかし、無残に失敗しました:私は配列にすべてuserWalletIDを追加し、そのようなゲッターとしてそれを渡すことで、自分で何かを書き込もうとしました私は個々のマッチングIDのそれぞれのバランスに合わせて更新を行う方法を知りません。例:

各マージンIDバランスを更新する最も良い方法は、「spendableConfirmedBalance」:2205867000です。

ご協力いただきありがとうございます。

+0

複数のドキュメントを更新したり、1つのクエリで異なる値を設定することはできません。各ユーザーを個別に更新する必要があります。 – Molda

+0

それを指摘してくれてありがとう。私はJSONに保存して戻ってくる前に、繰り返しの中に保存しようとしています。 –

答えて

0

私は1回の呼び出しでそれをやりました。それはすごくうまくいく、Mongoのやり方を指摘してくれたMoldaに感謝する。名誉!

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}); 
      }); 
     } 

    }); 
};