title: Mongoose bulkwrite update - how to push new obj into an array?
body: <p>I have a model that looks like:</p>
  
  <pre><code>fname: String,
  lname: String,
  rating: [{
      rating: {
        type: Number,
        enum: RATING,
        default: 5
      },
      date: {
        type: Date,
        default: Date.now
      }
  }]
  </code></pre>
  
  <p>I need to perform updates on this Model by adding new object inside the <code>rating array</code>, with new ratings and dates. I would like to use the <code>bulkwrite</code> method on <code>Model.collection</code> to do this because I need to enable bulk updates so that I don't have to update them one by one.</p>
  
  <p>I created an <code>array bulkUpdateOperations = []</code> and did the following in a loop:</p>
  
  <pre><code>bulkUpdateOperations.push({
        'updateOne': {
          'filter': {'_id': item.id},
          'update': {$push: {rating: {'rating': item.rating, 'date': Date.now}}}
        }
  });
  Person.collection.bulkWrite(bulkUpdateOperations, {orderd: true, w: 1}, callbackfunc);
  </code></pre>
  
  <p>But nothing gets updated. I get the following response:</p>
  
  <pre><code>...
  ...
  ...
  insertedCount: 0,
  matchedCount: 0,
  modifiedCount: 0,
  deletedCount: 0,
  upsertedCount: 0,
  upsertedIds: {},
  insertedIds: {},
  n: 0 }
  </code></pre>
  
  <p>I would be very thankful if someone helped me through this problem.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the array I'm sending as POST body to update the records:</p>
  
  <pre><code>[{
      "id": "5b7d4d348151700014d25bdd",
      "rating": 1
  },{
      "id": "5b771d10c1e03e1e78b854c2",
      "rating": 1
  },{
      "id": "5b771d7ac1e03e1e78b854c8",
      "rating": 1
  },{
      "id": "5b7bd75a33f88c1af8585be0",
      "rating": 1
  },{
      "id": "5b814a2322236100142ac9f6",
      "rating": 1
  }]
  </code></pre>
  
  <p>And here is a sample collection in the DB</p>
  
  <pre><code>{
      "_id": {
          "$oid": "5b7d4d348151700014d25bdd"
      },
      "status": "ACTIVE",
      "fname": "mr. client",
      "lname": "good client",
      "contact_info": {
          "_id": {
              "$oid": "5b7d4d348151700014d25bde"
          },
          "mobile_no": "0011223344",
          "phone_no": "11223344"
      },
      "handlers": [
          {
              "_id": {
                  "$oid": "5b7d4d348151700014d25bdf"
              },
              "date": {
                  "$date": "2018-08-22T11:47:00.544Z"
              },
              "id": {
                  "$oid": "5b7d45fbfb6fc007d8bdc1f4"
              }
          }
      ],
      "onboarding_date": {
          "$date": "2018-08-22T11:47:00.551Z"
      },
      "rating": [
      {
          "rating": 5,
          "_id": {
              "$oid": "5b814a8e22236100142ac9fc"
          },
          "date": {
              "$date": "2018-08-25T12:22:59.584Z"
          }
      },
      {
          "rating": 3,
          "_id": {
              "$oid": "5b814a8e22236100142ac9fb"
          },
          "date": {
              "$date": "2018-08-25T12:24:46.368Z"
          }
      }
  ],
      "__v": 0
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  Adding <code>upsert: true</code> as a filter for <code>updateOne</code> creates a new document with only <code>rating</code> as its value.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>replace</p>
  
  <pre><code>'filter': {'_id': item.id},
  </code></pre>
  
  <p>by</p>
  
  <pre><code>'filter': {'_id': mongoose.Types.ObjectId(item.id)},
  </code></pre>