0

I am trying to update multiple array element. I have data as follow:

{ 
    "_id" : ObjectId("5b1623343aeefb2a8973e26a"), 
    "userId" : ObjectId("5acf0e0e6d57ed7a22d16739"),      
    "previous" : [
        {
            "providerId" : ObjectId("5acf304b995ea23a707b58e5"),                 
            "_id" : ObjectId("5b1623343aeefb2a8973e286"), 
            "isBlocked" : true, 
        }, 
        {
            "providerId" : ObjectId("5acf304b995ea23a707b58e5"),                 
            "_id" : ObjectId("5b1623343aeefb2a8973e285"), 
            "isBlocked" : true,                
        }, 
        {
            "providerId" : ObjectId("5acf304b995ea23a707b58e5"),                
            "_id" : ObjectId("5b1623343aeefb2a8973e284"), 
            "isBlocked" : true, 
        }, 

I need to update isBlocked Status as false where providerId's are same.
I already followed step given by @Neil in the post below:

How to Update Multiple Array Elements in mongodb

Here i am sharing my query:

db.getCollection("collectionName").update(
{"userId" : ObjectId("5acf0e0e6d57ed7a22d16739")},
{$set:{"previous.$[elem].isBlocked":false}},
{
  arrayFilters:[{"elem.providerId":ObjectId("5acf304b995ea23a707b58e5")}]
}
)

If i am running this query mongo shell then it is working fine but it is not working in my code.

I am using MongoDB version 3.6 and my mongoose version is mongoose@4.13.9

Jitendra
  • 3,135
  • 2
  • 26
  • 42
  • Update Mongoose to a 5.x version. It's very high probability the `arrayFilters` is actually being stripped from the request to the server with any earlier version. You at least have the `ObjectId` in there, which is a requirement to cast manually with mongoose. I believe somebody might have actually warned about drivers which were not compatible with MongoDB 3.6 on the referenced answer. – Neil Lunn Jun 05 '18 at 11:42
  • Confirming for you that the code indeed fails on 4.x releases of Mongoose and requires an updated 5.x which bundles the updated mongodb node driver which accepts the `arrayFilters` argument without removing it. I actually mentioned this on an older answer which demonstrates the raw command form working with the server but the standard methods failing. – Neil Lunn Jun 05 '18 at 12:13
  • Also, remembering the question that got you here then setting "all" elements to `false` is simply `$set:{"previous.$[].isBlocked":false}` and does not need a matching `arrayFilters` at all. Hence that statement works with earlier drivers. – Neil Lunn Jun 05 '18 at 12:42
  • @NeilLunn , I do not want to set all element false. But i just tried it and it is throughing error "MongoError: Cannot create field 'isBlocked' in element........" – Jitendra Jun 05 '18 at 12:49
  • Does not matter anyway. You need to update mongoose and that solves all issues. – Neil Lunn Jun 05 '18 at 12:50
  • I already upgraded mongoose version to 5.0.5 – Jitendra Jun 05 '18 at 12:50
  • I'm just going to say once, that I have already run the same statement against your same document in both mongoose 4.13.9 and 5.1.4. The 5.1.4 works just fine and that's the latest version so I don't know how you can come to a lesser version. Install the latest version as the issue is closed and was even addressed on earlier versions. – Neil Lunn Jun 05 '18 at 12:52
  • after upgrading mongoose version now i there is no error but still it is not updating anything Err: Null , Result: { n: 1, nModified: 0, ok: 1 } – Jitendra Jun 05 '18 at 12:56

0 Answers0