简体   繁体   中英

How to avoid duplication in array of objects in a mongodb document

Here is the document sample:

{
    "_id": "658016",
    "playerName": "Kuldeep Singh",
    "tournamentId": "197831",
    "score": [
        {
            "_id": "1611380",
            "runsScored": 21,
            "ballFaced": 10,
            "fours": 3,
            "sixes": 1,
            "strikeRate": 210,
            "oversBowled": 3,
            "runsConceded": 34,
            "economyRate": 11.33,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "c Mayank b Nitin Gudle -(reddy)",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1602721",
            "runsScored": 21,
            "ballFaced": 14,
            "fours": 1,
            "sixes": 1,
            "strikeRate": 150,
            "oversBowled": 3,
            "runsConceded": 28,
            "economyRate": 9.33,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "not out",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1536540",
            "runsScored": 1,
            "ballFaced": 3,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 33.33,
            "oversBowled": 2,
            "runsConceded": 7,
            "economyRate": 3.5,
            "wickets": 3,
            "maiden": 0,
            "howToOut": "b Lalit",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1536483",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1501201",
            "runsScored": 12,
            "ballFaced": 5,
            "fours": 1,
            "sixes": 1,
            "strikeRate": 240,
            "oversBowled": 2,
            "runsConceded": 28,
            "economyRate": 14,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "c Rahul Mogri b Harshal Tupe",
            "catches": [
                "Chetan Tupe",
                "Kaustubh Tupe"
            ],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1501195",
            "runsScored": 74,
            "ballFaced": 27,
            "fours": 5,
            "sixes": 7,
            "strikeRate": 274.07,
            "oversBowled": 3,
            "runsConceded": 44,
            "economyRate": 14.67,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "not out",
            "catches": [
                "Ashish Thantharatey"
            ],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1461428",
            "runsScored": 8,
            "ballFaced": 4,
            "fours": 0,
            "sixes": 1,
            "strikeRate": 200,
            "oversBowled": 3,
            "runsConceded": 29,
            "economyRate": 9.67,
            "wickets": 2,
            "maiden": 0,
            "howToOut": "run out Mayank / Sahil Raheja",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1461413",
            "runsScored": 23,
            "ballFaced": 12,
            "fours": 0,
            "sixes": 3,
            "strikeRate": 191.67,
            "oversBowled": 2,
            "runsConceded": 18,
            "economyRate": 9,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "c Sumit Gandhi b Arun Stambhampelli",
            "catches": [
                "Arun Stambhampelli (c)"
            ],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1451183",
            "runsScored": 16,
            "ballFaced": 6,
            "fours": 2,
            "sixes": 1,
            "strikeRate": 266.67,
            "oversBowled": 2,
            "runsConceded": 11,
            "economyRate": 5.5,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "c Syed Jamaal Nasir b Aman Deep",
            "catches": [
                "Mohan Slathia (c)"
            ],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1451132",
            "runsScored": 26,
            "ballFaced": 10,
            "fours": 1,
            "sixes": 3,
            "strikeRate": 260,
            "oversBowled": 3,
            "runsConceded": 18,
            "economyRate": 6,
            "wickets": 3,
            "maiden": 0,
            "howToOut": "c Javed b Ram Lomte",
            "catches": [
                "Saheb Desai"
            ],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1392788",
            "runsScored": 13,
            "ballFaced": 6,
            "fours": 2,
            "sixes": 0,
            "strikeRate": 216.67,
            "oversBowled": 3,
            "runsConceded": 30,
            "economyRate": 10,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "not out",
            "catches": [
                "Sachin Patil"
            ],
            "stumping": [],
            "runout": [],
            "participatedRunout": [
                "Pankaj Rastogi"
            ]
        },
        {
            "_id": "1611380",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1602721",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1536540",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1501201",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1501195",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1461428",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1461413",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1451183",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1451132",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        },
        {
            "_id": "1392788",
            "runsScored": 0,
            "ballFaced": 0,
            "fours": 0,
            "sixes": 0,
            "strikeRate": 0,
            "oversBowled": 0,
            "runsConceded": 0,
            "economyRate": 0,
            "wickets": 0,
            "maiden": 0,
            "howToOut": "-",
            "catches": [],
            "stumping": [],
            "runout": [],
            "participatedRunout": []
        }
    ],
    "__v": 0
}

Note: in score array inside this document, there is duplication of objects after index 10.

The documents are initially updated (Before this, score array is empty)using this code:

await Scorecard.findOneAndUpdate(
        { _id: fielder.player_id, },
        {
          $addToSet: {
            "score": {
              _id: matchID
            }
          }
        }
      );

Later in code, objects inside score array are updated again to change default values(set in mongoose schema) of initally unchanged fields.

But, the problem is when the above quoted code runs again after sometime, it duplicates all the objects inside array. I know, there are other similar questions, but I am beginner in mongodb and not able to make complex aggregation pipelines. Any help is greatly appreciated.

The $addToSet operator only adds an element to the array if it does not already exist.

Each element in that array is an object. Objects are compared as described in the mongodb docs at https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#objects Basically, two objects are equal if they have exactly the same fields, in the exactly the same order, with exactly the same value for each. In other words, if they are not identical they are not equal.

I don't see any duplicates in the array in the question. There are some with the same value for the _id field, but those contain several other fields that are different, so they are not duplicates.

If you need to update the values of an object in the array, there are several other questions on modifying nested documents.

I suspect what you are trying to do is detect if an object with that id already exists in the array, update if it is preset, insert it if not.
In that situation, you might try using the pipeline form of update, with filter and mergeObjects operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM