Following the comments on Mongoose: how to define a combination of fields to be unique?
Asked
Active
Viewed 2,856 times
1 Answers
2
First let's get the array of data sorted by all values which supposed to be unique.
Assuming we're talking about strings (as in this question), we can combine them to create one long string that is supposed to be unique.
Being sorted, if there are duplicate values they'll show up right after the other, so let's look for results that repeat themselves:
var previousName;
Person.find().sort('firstName lastName').exec().each(function (person) {
var name = person.firstName + person.lastName;
if (name == previousName) {
console.log(name);
person.remove();
}
previousName = name;
})

Ronen Teva
- 1,345
- 1
- 23
- 43
-
I'm reviewing low quality posts. It's helpful to include a few sentences about how the code you posted addresses the issue. For example, here it looks like you concatenated the first and last names to form a unique value. – neontapir Jul 07 '16 at 18:48
-
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Code-only answers are discouraged. – Ajean Jul 07 '16 at 23:00
-
How much will it be effective and fast when data quantity is huge like in 10^4 to 10^6. – Soumyajit Dutta Feb 05 '18 at 06:54