2017-04-18 27 views
2

サブ文書の配列を含むモデルがあります。これはCompanyです:私はいくつかの企業を持っている、と私は私が探していphoneに一致するすべての企業内のすべての連絡先のフィールドchatIdを、更新したい複数のサブ文書をマングースとノードで更新する

{ 
"_id" : ObjectId(":58be7c236dcb5f2feff91ac0"), 
"name" : "sky srl", 

"contacts" : [ 
    { 
     "_id" : ObjectId("58be7c236dcb5f2feff91ac2"), 
     "name": { type: String, required: true }, 
     "company" : ObjectId("58be7c236dcb5f2feff91ac0"), 
     "email" : "[email protected]", 
     "chatId" : "", 
     "phone" : "123456789", 
     "name" : "John Smith" 
    }, 
    { 
     "_id" : ObjectId("58be7f3a6dcb5f2feff91ad3"), 
     "company" : ObjectId("58be7f3a6dcb5f2feff91ad1"), 
     "email" : "[email protected]", 
     "chatId" : "", 
     "phone" : "987654321", 
     "name" : "Bill Gaset" 
    } 
], 
    "__v" : 1 
} 

マイスキーマ定義(問題に焦点を当てるために、簡略化さ):

var contactSchema = new Schema({ 
[...] 
phone: { type: String, required: true }, 
email: { type: String }, 
chatId: { type: String }, 
company: Schema.Types.ObjectId, 
}); 

var companySchema = new Schema({ 

name: { type: String, required: true }, 
type: { type: String, default: "company" }, 
contacts: [contactSchema] 
}); 

私はこのanswer

var conditions = { "contacts.phone": req.body.phone }; 
var partialUpdate = req.body; //it contains 'req.body.phone' and 'req.body.chatId' attributes 
Company.find(conditions).then(
     function (results) { 
      results.map(function(companyFound) { 
       companyFound.contacts.forEach(function (contactContainer){ 

        if (contactContainer.phone == partialUpdate.phone) { 
         contactContainer.chatId = partialUpdate.chatId; 

         Company.save(); 
         companyFound.save(); 
         contactContainer.save(); 
         results.save(); 
        } 

        //not sure of what to save, so i save everything 
        companyFound.save(); 
        contactContainer.save(); 
        results.save(); 
       }); 
      }); 
}); 

を試してみました。それは動作しません。それは私が間違っていることは何も保存しませんか?

+0

ピンポイント。それはクエリか更新ですか? 'partialUpdate'とは何ですか?あなたのスキーマを表示することも良い考えかもしれません。 – Mikey

+0

OK、クエリで失敗しました。私が 'Company.find(条件).then(関数(結果){})'を使用すると、IDは失敗します。 私は 'partialUpdate' varを追加します、忘れました、ありがとう! – Diego

+0

私は試しましたが、 'var conditions = {" phone ":req.body.phone}; Contact.find(条件).then( 関数(結果){ はconsole.log( "結果:" ヌル+ JSON.stringify(結果、4)); }); ' 示します空の配列 – Diego

答えて

1

私は以前これをやったことはありませんが、試してみる価値があります。

おそらく$elemMatchを使用する必要があります。

// find the companies that have contacts having the phone number 
Company.find().where('contacts', { $elemMatch: { phone: req.body.phone }}).exec(function (err, companies) { 
    if (err) { 
     console.log(err); 
     return; 
    } 
    // see if you can at least get the query to work 
    console.log(companies); 
    async.eachSeries(companies, function updateCompany(company, done) { 
     // find and update the contacts having the phone number 
     company.contacts.forEach(function (contact, i, arr) { 
      if (contact.phone == req.body.phone) { 
       arr[i].chatId = req.body.chatId; 
      } 
     }); 
     company.save(done); 
    }, function allDone (err) { 
     console.log(err); 
    }); 
}); 

注:私はasync.jsを使用して、複数の項目に対して非同期操作を実行しています。

正直なところ、contactsという配列をContactreferencesの配列に変更するだけで、クエリと更新がはるかに簡単になりました。ただ、レコードの

+0

多くのおかげで@ミッキー! async.jsがなくてもちょっと修正しました(これは興味深いライブラリです)。 – Diego

1

:私はそれがasync.jsせずに動作させるためにこれをしなかった:それは最初に失敗した

Company.find().where('contacts', { $elemMatch: { phone: req.body.phone } }) 
      .exec(function (err, companies) { 
       if (err) { 
        console.log(err); 
        return; 
       } 
       console.log("companies: " + JSON.stringify(companies, null, 4)); 

       companies.forEach(function (company) { 
        company.contacts.map(function (contact, i, arr) { 
         if (contact.phone == req.body.phone) { 
          arr[i].telegramChatId = req.body.telegramChatId; 
         } 
        }); 
        company.save(); 
       }, 
       function allDone(err) { 
        console.log(err); 
       }); 
      });` 
関連する問題