查找具有相同值的重复项 MongoDB

发布于 2025-01-18 19:15:13 字数 498 浏览 0 评论 0原文

我有一个数据集,我想在多行中找到具有相同值的重复项。例如,我有一个包含12行的数据集,除了_id和标题值之外,底部2行具有相似的值。我该如何查找这些结果?好像我还不知道这些是重复的。 我的收藏是“销售”

[
  {
    _id: "C12",
    title: "blouse",
    price: 15,
    units_sold: 100,
    retail_price: 30,
    ad_boost: 1,
    rate_count: 34,
    rating: 4
  },
  {
    _id: "C10",
    title: "loose floral blouse",
    price: 15,
    units_sold: 100,
    retail_price: 30,
    ad_boost: 1,
    rate_count: 34,
    rating: 4
  }
]

I have a data set that I want to find the duplicates with the same values in multiple rows. For instance I have a data set that contains 12 different rows and I know the bottom 2 rows have similar values besides the _id and title values. How do I query to find these results? As if i dont already know these are duplicates.
My collection is 'sales'

[
  {
    _id: "C12",
    title: "blouse",
    price: 15,
    units_sold: 100,
    retail_price: 30,
    ad_boost: 1,
    rate_count: 34,
    rating: 4
  },
  {
    _id: "C10",
    title: "loose floral blouse",
    price: 15,
    units_sold: 100,
    retail_price: 30,
    ad_boost: 1,
    rate_count: 34,
    rating: 4
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-25 19:15:13

只需$ group,您将标识为重复检查密钥的所有字段。您可以$ push _id进入数组,以供以后获取/处理。

db.collection.aggregate([
  {
    $group: {
      _id: {
        price: "$price",
        units_sold: "$units_sold",
        retail_price: "$retail_price",
        ad_boost: "$ad_boost",
        rate_count: "$rate_count",
        rating: "$rating"
      },
      duplicate_ids: {
        $push: {
          _id: "$_id",
          title: "$title"
        }
      }
    }
  }
])

这是 mongo Playground 供您参考。

Simply $group by all the fields that you identify as duplicate check key. You can $push the _id into an array for later fetching/processing.

db.collection.aggregate([
  {
    $group: {
      _id: {
        price: "$price",
        units_sold: "$units_sold",
        retail_price: "$retail_price",
        ad_boost: "$ad_boost",
        rate_count: "$rate_count",
        rating: "$rating"
      },
      duplicate_ids: {
        $push: {
          _id: "$_id",
          title: "$title"
        }
      }
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文