0

I have a data in MongoDB and I want to remove all the duplicated Documents. The problem is MongoDB assigns each item a unique _id so I can't just delete the duplicated items. I have a field in each Document called name and I want to delete the items that have the same name. For example:

[{_id: 5c7e423f0bdaa9aeb5399e90,
name ="A"
grade = 16
enrolled ="dddh"
},

{_id: 5c7e423f1bdaa9aeb5399e90,
key ="B"
grade =17
note ="ddddd"
},

{_id: 5c7e423d0bdaa9aeb5399e90,
key ="B"
score =17
note ="ddddd"
}]

to:

[{_id: 5c7e423f0bdaa9aeb5399e90,
name ="A"
grade = 16
enrolled ="dddh"
},

{_id: 5c7e423f1bdaa9aeb5399e90,
name ="B"
grade =17
enrolled ="ddddd"
}]

The list might be big so is there any efficient way to do it?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Davvvvad
  • 95
  • 1
  • 10
  • Note as you state yourself *"MongoDB assigns each item a unique _id"* but you posted the question with data showing the same `_id` values due to "copy/paste". This could be ( and was ) confusing to people viewing the question. Also Documents in MongoDB are not JSON, which further confuses things when you use that term – Neil Lunn Mar 08 '19 at 22:56
  • _id values are differenf in the qeustion. Also I'm asking about JSON array. Documents in MongoDB are not JSON but after I processed them with JS they became JSON. – Davvvvad Mar 08 '19 at 23:05
  • Read the links. You can return the data with the "duplicates" removed – Neil Lunn Mar 08 '19 at 23:07
  • I have read the links previously and it didn't work in my query because `distinct()` doesn't work with `sort()` and `find()` – Davvvvad Mar 08 '19 at 23:14

1 Answers1

0

First note your structure contains a lot of syntax errors. Now, one solution is to use Array.reduce() over your original array of objects to generate a new object using the name properties as the keys of this new object. This will ensure you will end up with only one object associated per name property. Finally, you can use Object.values() to get your desired output.

let input = [
  {
    _id: "5c7e423f0bdaa9aeb5399e90",
    name: "A",
    grade: 16,
    enrolled: "dddh"
  },
  {
    _id: "5c7e423f1bdaa9aeb5399e90",
     name: "B",
     grade: 17,
     note: "ddddd"
  },
  {
    _id: "5c7e423d0bdaa9aeb5399e90",
    name: "B",
    score: 17,
    note: "ddddd"
  }
];

let res = input.reduce((acc, curr) =>
{
    acc[curr.name] = acc[curr.name] || curr;
    return acc;
}, {});

console.log(Object.values(res));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Shidersz
  • 16,846
  • 2
  • 23
  • 48
  • The question is poorly titled. It's actually asking about "documents in a MongoDB collection" and not an "Array of Objects in JavaScript", which is what you have answered. – Neil Lunn Mar 08 '19 at 22:51
  • "Array of Objects in JavaScript" is exactly what I am asking... The answer worked perfectly. Thank you. – Davvvvad Mar 08 '19 at 23:54