Search code examples
node.jsmongodbmongoosebulkupdate

Mongoose bulkwrite update - how to push new obj into an array?


I have a model that looks like:

fname: String,
lname: String,
rating: [{
    rating: {
      type: Number,
      enum: RATING,
      default: 5
    },
    date: {
      type: Date,
      default: Date.now
    }
}]

I need to perform updates on this Model by adding new object inside the rating array, with new ratings and dates. I would like to use the bulkwrite method on Model.collection to do this because I need to enable bulk updates so that I don't have to update them one by one.

I created an array bulkUpdateOperations = [] and did the following in a loop:

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

But nothing gets updated. I get the following response:

...
...
...
insertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {},
n: 0 }

I would be very thankful if someone helped me through this problem.

EDIT

Here is the array I'm sending as POST body to update the records:

[{
    "id": "5b7d4d348151700014d25bdd",
    "rating": 1
},{
    "id": "5b771d10c1e03e1e78b854c2",
    "rating": 1
},{
    "id": "5b771d7ac1e03e1e78b854c8",
    "rating": 1
},{
    "id": "5b7bd75a33f88c1af8585be0",
    "rating": 1
},{
    "id": "5b814a2322236100142ac9f6",
    "rating": 1
}]

And here is a sample collection in the DB

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

EDIT Adding upsert: true as a filter for updateOne creates a new document with only rating as its value.

SOLUTION

replace

'filter': {'_id': item.id},

by

'filter': {'_id': mongoose.Types.ObjectId(item.id)},

Solution

  • changing

    bulkUpdateOperations.push({
          'updateOne': {
            'filter': {'_id': item.id},
            'update': {$push: {rating: {'rating': item.rating, 'date': Date.now}}}
          }
    });
    

    to

    bulkUpdateOperations.push({
          'updateOne': {
            'filter': {'_id': mongoose.Types.ObjectId(item.id)},
            'update': {$push: {rating: {'rating': item.rating, 'date': Date.now}}}
          }
    });
    

    worked. Notice the type cast I had to manually perform in

    'filter': {'_id': mongoose.Types.ObjectId(item.id)},
    

    I thought mongoose would automatically cast the string to an ObjectId type, but maybe because I'm dropping down a level of abstraction by using Person.collection, mongoose did not auto-cast the itemId.

    Please feel free to update this answer if anyone can confirm why I had to cast the string manually.