MongoDB-使用聚合展开数组并删除重复项 [英] MongoDB - Unwind array using aggregation and remove duplicates

查看:160
本文介绍了MongoDB-使用聚合展开数组并删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB聚合框架展开数组,并且该数组具有重复项,在进一步分组时,我需要忽略这些重复项.

I am unwinding an array using MongoDB aggregation framework and the array has duplicates and I need to ignore those duplicates while doing a grouping further.

我该如何实现?

推荐答案

,您可以使用 $ addToSet 为此:

db.users.aggregate([
  { $unwind: '$data' },
  { $group: { _id: '$_id', data: { $addToSet: '$data' } } }
]);

很难在不看到实际查询的情况下为您提供更具体的答案.

It's hard to give you more specific answer without seeing your actual query.

这篇关于MongoDB-使用聚合展开数组并删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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