Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I have a collection in MongoDB, which is like the following:

{

    "_id" : "5327010328645530500",

    "members" : [

        {

            "participationCoeff" : 1,

            "tweetID" : "5327010328645530500"

        },

        {

            "participationCoeff" : 1,

            "tweetID" : "2820402625046999289"

        },

        {

            "participationCoeff" : 0.6666666666666666,

            "tweetID" : "6122060484520699114"

        },

        {

            "participationCoeff" : 1,

            "tweetID" : "4656669980325872747"

        }

    ]

}

{

    "_id" : "2646953848367646922",

    "members" : [

        {

            "participationCoeff" : 1,

            "tweetID" : "2646953848367646922"

        },

        {

            "participationCoeff" : 0.75,

            "tweetID" : "7750833069621794130"

        },

        {

            "participationCoeff" : 0.5,

            "tweetID" : "6271782334664128453"

        }

    ]

}

The collection has clusters, where a cluster has an _id field and a member field. Members field is an array of documents, having the following format.

{

            "participationCoeff" : 1,

            "tweetID" : "5327010328645530500"

        }

Now, from time to time, I have to delete these sub-documents in member's attributes of cluster document, by matching tweetID.

However, I cannot find a query that makes it possible to achieve this effect. A tweetID will participate in many clusters, and hence, it will appear in multiple sub-documents of the 'members' attribute of different clusters. I want to run a bulk $pull operation so that I can remove all the sub-documents from all the clusters (i.e., their members attribute) that match on a particular tweetID.

Some help and intuition will be really helpful.

1 Answer

0 votes
by (107k points)

For applying a bulk $pull operation, just set the ‘multi’ parameter as true like:

{multi:true}

In MongoDB, if you will not set multi as true, then the pull operation will only remove the first matched document.

db.clusters.update({}, 

    {$pull: {members: {tweetID: '5327010328645530500'}}}, 

    {multi: true})

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...