获取数组字段中所有唯一值的集合 [英] Get the set of all unique values in array field

查看:33
本文介绍了获取数组字段中所有唯一值的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下文件:

{ "_id" : ObjectId("585901b7875bab86885cf54f"), "foo" : 24, "bar" : [ 1, 2, 5, 6 ] }
{ "_id" : ObjectId("585901be875bab86885cf550"), "foo" : 42, "bar" : [ 3, 4 ] }

我想获取 bar 字段中的所有唯一值,例如:

I want to get all the unique values in the bar field, something like:

{"_id": "something", "bar": [1, 2, 3, 4, 5, 6]}

这是我试过的:

db.stuff.aggregate([{
  $group: {
    _id: null, 
    bar: {
      $addToSet: {$each: "$bar"}
    }
  }
}])

但抱怨 $each 不是公认的运算符.

But complains that $each is not a recognized operator.

这确实有效:

db.stuff.aggregate([{
  $group: {
    _id: null, 
    bar: {
      $addToSet: "$bar"
    }
  }
}])

但显然产生了错误的结果:

But obviously produces a wrong result:

{ "_id" : null, "bar" : [ [ 3, 4 ], [ 1, 2, 5, 6 ] ] }

<小时>

编辑

通过添加第一个 $unwind 阶段,我设法得到了我想要的结果:

I managed to have the result I want by adding a first $unwind stage:

db.stuff.aggregate([{
  $unwind: { "$bar" },
  $group: {
    _id: null, 
    bar: {
      $addToSet: "$bar"
    }
  }
}])

=> { "_id" : null, "bar" : [ 4, 3, 5, 2, 6, 1 ] }

是否有可能在单个流水线阶段实现?

Is it possible at all to make it in one single pipeline stage?

推荐答案

distinct() 也适用于数组字段,因此可以很好地做到这一点.

The distinct() works with array field as well so will beautifully do this.

db.stuff.distinct('bar')

聚合框架在这方面太过分了,不会表现得很好

The aggregation framework is overkill for this and will not perform well

这篇关于获取数组字段中所有唯一值的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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