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" : "[email protected]",
      "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} });
1

1 Answer 1

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" : "[email protected]",
                        "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" : "[email protected]",
                        "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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.