freeCodeCamp/curriculum/challenges/japanese/05-back-end-development-and.../mongodb-and-mongoose/perform-new-updates-on-a-do...

2.2 KiB

id title challengeType forumTopicId dashedName
587d7fb8367417b2b2512c0f Model.findOneAndUpdate() を使用して、ドキュメントの新しい更新を実行する 2 301542 perform-new-updates-on-a-document-using-model-findoneandupdate

--description--

最新バージョンの Mongoose は、ドキュメントの更新を簡素化するメソッドを備えています。 しかし、いくつかの高度な機能 (プレ/ポストフック、検証) は、上記のアプローチに対しては異なる動作をします。そのため、従来のメソッドが今でも多くの状況で役に立ちます。 findByIdAndUpdate() は、id で検索するときに使用できます。

--instructions--

findAndUpdate 関数を変更して、Name で個人を検索し、年齢を 20 に設定してください。 検索キーとして、関数パラメーター personName を使用してください。

注: 更新したドキュメントを返す必要があります。 そのためには、オプションのドキュメント { new: true }findOneAndUpdate() の第 3 引数として渡す必要があります。 デフォルトでは、これらのメソッドは変更前のオブジェクトを返します。

--hints--

アイテムを正しく findOneAndUpdate する必要があります。

(getUserInput) =>
  $.post(getUserInput('url') + '/_api/find-one-update', {
    name: 'Dorian Gray',
    age: 35,
    favoriteFoods: ['unknown']
  }).then(
    (data) => {
      assert.equal(data.name, 'Dorian Gray', 'item.name is not what expected');
      assert.equal(data.age, 20, 'item.age is not what expected');
      assert.deepEqual(
        data.favoriteFoods,
        ['unknown'],
        'item.favoriteFoods is not what expected'
      );
      assert.equal(
        data.__v,
        0,
        'findOneAndUpdate does not increment version by design!'
      );
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

--solutions--

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/