如何使用猫鼬列出和删除重复值 [英] How to list and remove duplicate values using mongoose

查看:70
本文介绍了如何使用猫鼬列出和删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 猫鼬上的评论: 如何定义一个字段的组合是唯一的?

推荐答案

首先让我们得到按所有值排序的数据数组,这些值应该是唯一的.
假设我们谈论的是字符串(如本问题中所示),我们可以将它们组合起来创建一个应该是唯一的长字符串.
排序后,如果有重复的值,它们会一个接一个地出现,所以让我们寻找重复的结果:

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;
})

这篇关于如何使用猫鼬列出和删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆