1

My collection named user have 6 fields. Profile field is document. It have only array named Packages. Packages is collection of documents, which contains all packages current user have.

My database is like:

{
  "_id" : "AayujR3SLT5MtmTKf",
  "createdAt" : ISODate("2015-09-18T07:19:05.069Z"),
  "services" : {
    "password" : {
      "bcrypt" : "encripted_password_here"
    }
  },
  "username" : "test_user",
  "emails" : [{
      "address" : "1@gmail.com",
      "verified" : false
    }],
  "profile" : {
    "packages" : [{
        "packageId" : "67fmCMNTdqejFs7NE",
        "name" : "package1"
        "active" : true
      }, {
        "packageId" : "Dcn4PkmHNe8APuk73",
        "name" : "package2"
        "active" : true
      }, {
        "packageId" : "yvdXkPeNHEWwwLKjC",
        "name" : "package2"
        "active" : true
      }]
  }
}

I want set all active to false. What should I do? My current code is like (not working):

Meteor.users.update({ _id: Session.get('user_id') }, { $set: {'profile.packages.active': false} });
Gereltod
  • 2,043
  • 8
  • 25
  • 39
  • Possible duplicate of [MongoDB update multiple records of array](http://stackoverflow.com/questions/14720734/mongodb-update-multiple-records-of-array) – Michel Floyd Sep 23 '15 at 05:44

1 Answers1

1

It cannot be done in a single query as there are many elements in the "packages" array. Even if you try the below query, only the first match will get updated in the current document and rest will remain same.

db.exp10.update({"profile.packages.active":true},{$set:{"profile.packages.$.active":false}},{multi:true})

Output :

{
        "_id" : "AayujR3SLT5MtmTKf",
        "createdAt" : ISODate("2015-09-18T07:19:05.069Z"),
        "services" : {
                "password" : {
                        "bcrypt" : "encripted_password_here"
                }
        },
        "username" : "test_user",
        "emails" : [
                {
                        "address" : "1@gmail.com",
                        "verified" : false
                }
        ],
        "profile" : {
                "packages" : [
                        {
                                "packageId" : "67fmCMNTdqejFs7NE",
                                "name" : "package1",
                                "active" : false
                        },
                        {
                                "packageId" : "Dcn4PkmHNe8APuk73",
                                "name" : "package2",
                                "active" : true
                        },
                        {
                                "packageId" : "yvdXkPeNHEWwwLKjC",
                                "name" : "package2",
                                "active" : true
                        }
                ]
        }
}

So better we can do it using the below piece of code :

db.user.find({"profile.packages.active":true}).forEach(function(doc){
for( var count = 0; count < doc.profile.packages.length; count++ )
{
    if( doc.profile.packages[count].active == true )
        doc.profile.packages[count].active = false;     
}
db.user.save(doc);
});

Output :

{
        "_id" : "AayujR3SLT5MtmTKf",
        "createdAt" : ISODate("2015-09-18T07:19:05.069Z"),
        "services" : {
                "password" : {
                        "bcrypt" : "encripted_password_here"
                }
        },
        "username" : "test_user",
        "emails" : [
                {
                        "address" : "1@gmail.com",
                        "verified" : false
                }
        ],
        "profile" : {
                "packages" : [
                        {
                                "packageId" : "67fmCMNTdqejFs7NE",
                                "name" : "package1",
                                "active" : false
                        },
                        {
                                "packageId" : "Dcn4PkmHNe8APuk73",
                                "name" : "package2",
                                "active" : false
                        },
                        {
                                "packageId" : "yvdXkPeNHEWwwLKjC",
                                "name" : "package2",
                                "active" : false
                        }
                ]
        }
}

P.S :

If the collection has many documents, For better performance we can use Bulk Operations.

Yathish Manjunath
  • 1,919
  • 1
  • 13
  • 23